Tuesday, November 02, 2010

End To End example of how to HTTP Get relatively short stuff using HTTPClient

Abstract:

When HTTP GETting a lot of small stuff connection reuse is useful.

Use of multiple threads for download is very common.
So here an example of How to pass HTTP connection in such multithreaded environment:

Limitations:

It tested & worked on Android 2.2.

links:

HTTPClient
sample from apache.org

-----------------------------------------------------------------------------

static public class HTTPConectionProvider
{
    private static final int MAX_TOTAL_CONNECTIONS = 100;
    private static final HTTPConectionProvider instance = new HTTPConectionProvider();
    private final HttpClient httpClient ;
    private HTTPConectionProvider()
    {
        // Create and initialize HTTP parameters
        HttpParams params = new BasicHttpParams();
        ConnManagerParams.setMaxTotalConnections(params, MAX_TOTAL_CONNECTIONS);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
        // Create and initialize scheme registry
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(
                new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
        // Create an HttpClient with the ThreadSafeClientConnManager.
        // This connection manager must be used if more than one thread will
        // be using the HttpClient.
        httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params);
    } 
    public static HTTPConectionProvider get(){return instance;}
    public HttpClient getHttpClient(){return httpClient;}
} 


static public byte[] getByteArrayFromHTTP(String url) throws ClientProtocolException, IOException
{
    final String tag = "getByteArrayFromHTTP";
    final HttpClient httpClient = HTTPConectionProvider.get().getHttpClient();
    final HttpContext httpContext = new BasicHttpContext();
    final long ThreadID = Thread.currentThread().getId();
    HttpGet httpget = null;
    httpget = new HttpGet(url);
    Log.v(tag, "T[" + ThreadID + "]: about to get something from " + httpget.getURI());
    // execute the method
    HttpResponse response;
        response = httpClient.execute(httpget, httpContext);
    Log.v(tag, "T[" + ThreadID + "]: get executed");
    // get the response body as an array of bytes
    HttpEntity entity = response.getEntity();
    if (entity != null)
    {
        byte[] bytes = EntityUtils.toByteArray(entity);
        Log.v(tag, "T[" + ThreadID + "] - " + bytes.length + " bytes read from " + url);
        if (bytes.length == 0)
        {
            if (httpget != null)
                httpget.abort();
            Log.e(tag, "T[" + ThreadID + "]: error: Coudn't download " + url);
            return null;
        }
        return bytes;
    }
    if (httpget != null)
        httpget.abort();
    Log.e(tag, "T[" + ThreadID + "]: error: no message entity in response from " + url);
    return null;
}


-----------------------------------------------------------------------------

No comments:

Followers