Create Http connection : Http Connection « Network « Android

Create Http connection


package app.test;


import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.os.Bundle;
import android.util.Log;

class ApplicationEx extends Application {
  String TAG = "ApplicationEx";
  private AndroidHttpClient httpClient;

  public void onCreate() {
    httpClient = createHttpClient();

  private AndroidHttpClient createHttpClient() {
    AndroidHttpClient httpClient = AndroidHttpClient.newInstance("Mozilla/5.0 (Linux; U; Android 2.1; en-us; ADR6200 Build/ERD79) AppleWebKit/530.17 (KHTML, like Gecko) Version/ 4.0 Mobile Safari/530.17");

    ClientConnectionManager conMgr = httpClient.getConnectionManager();
    SchemeRegistry schReg = conMgr.getSchemeRegistry();
    for (String scheme : schReg.getSchemeNames()) {
      Log.v(TAG,"Scheme: "+ scheme+ ", port: "
              + schReg.getScheme(scheme).getDefaultPort()
              + ", factory: "
              + schReg.getScheme(scheme).getSocketFactory()

    HttpParams params = httpClient.getParams();
    Log.v(TAG,"http.protocol.version: "+ params.getParameter("http.protocol.version"));
    Log.v(TAG,"http.protocol.content-charset: "+ params.getParameter("http.protocol.content-charset"));
    Log.v(TAG,"http.protocol.handle-redirects: "+ params.getParameter("http.protocol.handle-redirects"));
    Log.v(TAG,"http.conn-manager.timeout: "+ params.getParameter("http.conn-manager.timeout"));
    Log.v(TAG,"http.socket.timeout: "+ params.getParameter("http.socket.timeout"));
    Log.v(TAG,"http.connection.timeout: "+ params.getParameter("http.connection.timeout"));
    return httpClient;

  public AndroidHttpClient getHttpClient() {
    if (httpClient == null)
      httpClient = createHttpClient();
    return httpClient;

  public void onLowMemory() {

  public void onTerminate() {

  private void shutdownHttpClient() {
    if (httpClient != null) {
      if (httpClient.getConnectionManager() != null) {
      httpClient = null;

public class Test extends Activity {
  private ApplicationEx app;
  private HttpClient httpClient;

  public void onCreate(Bundle savedInstanceState) {

    app = (ApplicationEx) this.getApplication();
    httpClient = app.getHttpClient();

  public void getHttpContent() {
    try {
      HttpGet request = new HttpGet("");
      HttpParams params = new BasicHttpParams();
      HttpConnectionParams.setSoTimeout(params, 60000); // 1 minute
      Log.v("connection timeout", String.valueOf(HttpConnectionParams
      Log.v("socket timeout",

      String page = httpClient.execute(request,
          new BasicResponseHandler());
    } catch (IOException e) {
      // covers:
      // ClientProtocolException
      // ConnectTimeoutException
      // ConnectionPoolTimeoutException
      // SocketTimeoutException


Related examples in the same category

1.Http Connection
2.Using HttpGet to get the web response
3.Http connection with
4.HttpGet and DefaultHttpClient
5.Http Post
6.Simple HTTP Request
7.Http Request Class
8.Http Get and Http Post
9.Get Text from HttpResponse
10.Http Request
11.Http Downloader
12.Is Http Url Available
13.Http Retriever
14.Receive Response from HttpURLConnection
15.Print http headers. Useful for debugging.
16.Return the base URL from the given URL. Example: ->
17.Send message with HttpPost
18.Get Http Stream
19.Generic Object Http Loader
20.Http Get and DefaultHttpClient
21.Gets http output from URL
22.Util for Http Get Http Get Request and return the status code
24.Http Get
25.implements HttpClient
26.Get File From HTTP
27.Make all redirects to go to http in stead of https
28.New Http Client Manager
29.Http Client Manager
30.Multipart Post
31.Get Server Data
32.Yahoo News Crawler
33.Send Packet
34.Read a web page
35.parse Request Header
36.Data Send Utils
37.This class is in charge of synchronizing the events (with due dates) with Google Calendar
38.Update Favicon
39.Converts key-value pair to appropriate signature for Facebook.