To send a HTTP GET request and retrieve response:
private static void get(final Context context, final String url) { new Thread() { @Override public void run() { HttpClient client = new DefaultHttpClient(); HttpGet request = Utils.createHttpRequest(context, url, true, false); setProxyIfNecessary(context, request); try { HttpResponse response = client.execute(request); Log.v("Test", "StatusCode: " + response.getStatusLine().getStatusCode() + ", Entity: " + EntityUtils.toString(response.getEntity())); } catch (Exception e) { // Oh, crash } } }.start(); } private static void setProxyIfNecessary(Context context, HttpUriRequest request) { ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivity == null ? null : connectivity.getActiveNetworkInfo(); if (networkInfo == null || networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { return; } String proxyHost = Proxy.getHost(context); if (proxyHost == null) { return; } int proxyPort = Proxy.getPort(context); if (proxyPort < 0) { return; } HttpHost proxy = new HttpHost(proxyHost, proxyPort); ConnRouteParams.setDefaultProxy(request.getParams(), proxy); }
Call it by:
get(context, "http://developer.android.com/index.html");
And you will get log by logcat *:S Test:V
Notice that the new thread is to avoid blocking UI thread