Create Http connection
package app.test;
import java.io.IOException;
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.app.Activity;
import android.app.Application;
import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import android.util.Log;
class ApplicationEx extends Application {
String TAG = "ApplicationEx";
private AndroidHttpClient httpClient;
@Override
public void onCreate() {
super.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()
.getClass().getName());
}
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;
}
@Override
public void onLowMemory() {
super.onLowMemory();
shutdownHttpClient();
}
@Override
public void onTerminate() {
super.onTerminate();
shutdownHttpClient();
}
private void shutdownHttpClient() {
if (httpClient != null) {
if (httpClient.getConnectionManager() != null) {
httpClient.getConnectionManager().shutdown();
}
httpClient.close();
httpClient = null;
}
}
}
public class Test extends Activity {
private ApplicationEx app;
private HttpClient httpClient;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (ApplicationEx) this.getApplication();
httpClient = app.getHttpClient();
getHttpContent();
}
public void getHttpContent() {
try {
HttpGet request = new HttpGet("http://www.google.com/");
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 60000); // 1 minute
request.setParams(params);
Log.v("connection timeout", String.valueOf(HttpConnectionParams
.getConnectionTimeout(params)));
Log.v("socket timeout",
String.valueOf(HttpConnectionParams.getSoTimeout(params)));
String page = httpClient.execute(request,
new BasicResponseHandler());
System.out.println(page);
} catch (IOException e) {
// covers:
// ClientProtocolException
// ConnectTimeoutException
// ConnectionPoolTimeoutException
// SocketTimeoutException
e.printStackTrace();
}
}
}
Related examples in the same category