extends Application : Application « Core Class « Android






extends Application

  


package app.test;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
class MyHttpClient extends Application {
  private HttpClient httpClient;
  @Override
  public void onCreate() {
    super.onCreate();
    httpClient = createHttpClient();
  }
  private HttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params,HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);

    ConnManagerParams.setTimeout(params, 1000);

    HttpConnectionParams.setConnectionTimeout(params, 5000);
    HttpConnectionParams.setSoTimeout(params, 10000);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory
        .getSocketFactory(), 80));
    schReg.register(new Scheme("https",
        SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
        params, schReg);

    return new DefaultHttpClient(conMgr, params);
  }

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

  @Override
  public void onLowMemory() {
    super.onLowMemory();
    shutdownHttpClient();
  }

  @Override
  public void onTerminate() {
    super.onTerminate();
    shutdownHttpClient();
  }

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

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

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    app = (MyHttpClient) this.getApplication();
    httpClient = app.getHttpClient();
    getHttpContent();
  }

  public void getHttpContent() {
    try {
      HttpGet request = new HttpGet("http://www.google.com/");
      HttpParams params = request.getParams();
      HttpConnectionParams.setSoTimeout(params, 60000); // 1 minute
      request.setParams(params);
      String page = httpClient.execute(request,
          new BasicResponseHandler());
      System.out.println(page);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

   
    
  








Related examples in the same category

1.Application Widget
2.Loads the list of installed applications in mApplications.
3.Implementation of the android.app.Instrumentation class, allowing you to run tests against application code.
4.This application demonstrates the seeking capability of ValueAnimator.
5.Get Application File Path
6.Creates a file storing a UUID on the first application start. This UUID can then be used as a identifier of this specific application installation.