Replacement for deprecated DefaultHttpClient

As well as linking to that blog, the docs recommend you use URL.openConnection which has been around since API level 1.

Alternative


Option 1 : Volley

Android 1.6 (API Level 4) or higher

Volley's benefits :

  • Automatic scheduling of network requests.
  • Multiple concurrent network connections.
  • Transparent disk and memory response caching with standard HTTP cache coherence.
  • Support for request prioritization.
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
  • Ease of customization, for example, for retry and backoff.
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  • Debugging and tracing tools.

Option 2 : OkHttp

OkHttp supports Android 2.3 and above. For Java, the minimum requirement is 1.7.

Some of the features listed in its documentation :

  • HTTP/2 and SPDY support allows all requests to the same host to share a socket.
  • Connection pooling reduces request latency (if SPDY isn’t available).
  • Transparent GZIP shrinks download sizes.
  • Response caching avoids the network completely for repeat requests.

Ok I waited for a week and so and did all lots of researches. I think I have found the answer.

I strongly advise for beginners and even professional Android programmers to know the existence of very helpful library called Retrofit:

Extensive document is present at http://square.github.io/retrofit/

The Stack overflow also has samples for almost everything one need to do over a network to contact a remote REST service.

It is better to stop using HttpURLConnection and AsyncTask. Retrofit is way faster and supports all failure situations out of the box.


You should switch to HttpURLConnection. It requires slightly more code, but not so much. In SDK 22 they already deprecated HttpClient, and even if you target SDK 19, when compiling with 22 you get warnings.

Just speculation (cannot find any source for this now) but I guess they will not do any fixing in the deprecated libraries, so any security problems or bugs will remain unfixed.