Tag Archives: HttpClient

Http Clients in Android

When you come to use HTTP you may wonder which class to use to send and receive data.

There are 2 main

Options:

Which one to use?

Apache http clients are big and flexible implementations, however exactly because of this reason (complexity) they are not actively supported by Android team.

HttpURLConnection on the other hand is lighter and can serve the needs of most apps. It is also continuously improved by the Android team. Since Android 4.0 (Ice Cream Sandwich) it also  includes a response cache. This is quite handy however it is include from API 13 so to enabled it without impact earlier versions reflection can be used:

try {
           File httpCacheDir = new File(context.getCacheDir(), "http");
           long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
           Class.forName("android.net.http.HttpResponseCache")
                   .getMethod("install", File.class, long.class)
                   .invoke(null, httpCacheDir, httpCacheSize);
        catch (Exception httpResponseCacheNotAvailable) {
       }}

 Finally

As also stated in this post:

Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.

For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.

For more info on Http check http://web.djodjo.org/article/articles/android_articles/dev/web-and-services/http_access