Monday, May 16, 2016

Apache HttpClient and HttpUrlConnection for android

APACHE HTTP CLIENT AND HTTP URL CONNECTION FOR ANDROID:

Http client and HttpUrlConnection both are used to make connection over HTTP to communicate through server in android. From android M (6.0) apache http client is officially deprecated from android and from gingerbread (2.3) it is suggested to use HttpUrlConnection instead of HttpClient .

Quote from android official site “Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpUrlConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption.”

HTTP CLIENT:

Http client project represents an effort to provide an implementation of Apache HttpClient, which can be deployed on Google Android in parallel to the outdated version shipped with the platform while remaining partially API compatible with Apache HttpClient 4.3.

There is few changes that has to be different from the stock version of apache HttpClient and android version of apache HttpClient.
1.       Compiled against HttpClient 4.0 APIs.
2.       Commons Logging replaced with Android Logging.
3.       Base64 implementation from Commons Codec replaced with Android Base64.
4.       Android default SSLSocketFactory used by for SSL/TLS connections.

There is few things about http client that is good for web browser but it is not as good as for android.
Android operate on small devices there for it require less battery consumption, small API for memory purpose etc.
1.       Large and extensive APIs
2.       Supports cookie handling, authentication and connection management
3.       More suitable for web browser and other web applications, because of its large size
4.       Does not support HttpResponseCache mechanism, hence leading to increased network usage and battery consumption.

HTTP URLCONNECTION:

HttpUrlConnection is a light weight HTTP client, used to access network resources, and hence, suitable for mobile applications and recommended by Google to use HttpUrlConnection, instead of the other HTTP clients.

According to google blog Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases. For Gingerbread, HttpURLConnection is the best choice. It’s 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.”

Advantage of HttpUrlConnection that is useful for android

1.       Light weight APIs helps in easier management and reduces compatibility issues.
2.       Automatic handling of the caching mechanisms, with the help of HttpResponseCache.
3.       Reduces the network usage and also, the battery consumption.





8 comments:

  1. Clear explanation. Nice blog

    ReplyDelete