12

I am using DefaultHttpClient in my current app.

I read this article which states that the DefaultHttpClient is deprecated: http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html

It points to this website: http://android-developers.blogspot.com/2011/09/androids-http-clients.html?m=1 which is too old and written in 2011.

I am going to drop using DefaultHttpClient and follow this article which uses Apache’s HttpClient: http://loopj.com/android-async-http/

I wonder if this is the right path to take when programming in 2015 targeting android API 19 and above.

weston
  • 54,145
  • 21
  • 145
  • 203
Dr. Ehsan Ali
  • 4,735
  • 4
  • 23
  • 37
  • the page you link to says: `An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries`, but wait... you already said that Apache’s HttpClient is deprecated so...? – pskink Aug 04 '15 at 06:48
  • 1
    btw: did you hear about https://developer.android.com/training/volley/index.html, http://square.github.io/okhttp/ and similar libs? – pskink Aug 04 '15 at 06:53
  • 1
    Ok it is then even worse if both are the same.http://loopj.com/android-async-http/ is deprecated too then. What do people use nowadays then? – Dr. Ehsan Ali Aug 04 '15 at 06:54
  • volley, okhttp and such, see my comment above – pskink Aug 04 '15 at 06:56
  • 1
    Never heard of Volley nor okhttp. I see another guy posted an answer mentioning them. – Dr. Ehsan Ali Aug 04 '15 at 07:00

6 Answers6

11

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.

Dr. Ehsan Ali
  • 4,735
  • 4
  • 23
  • 37
  • 1
    I prefer Volley over Retrofit (I hate the annotations that Retrofit uses). Check it out at http://developer.android.com/training/volley/index.html – FractalBob May 04 '16 at 01:48
5

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

Alternative

weston
  • 54,145
  • 21
  • 145
  • 203
3

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.

Sebastian
  • 1,076
  • 9
  • 24
2

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.
Faiz Siddiqui
  • 2,623
  • 1
  • 15
  • 15
  • 1
    What's the underlying library for the above options ? I guess they might using the same deprecated Apache client library? – Dr. Ehsan Ali Aug 04 '15 at 06:58
  • OkHttp and HttpUrlConnection are low-level HTTP clients. Volley is a high-level client that wraps OkHttp or HttpUrlConnection. – Faiz Siddiqui Aug 04 '15 at 07:29
0

It's Time to switch over to OkHttp which does HTTP efficiently and makes your stuff load faster and saves bandwidth.It supports both synchronous blocking calls and async calls with callbacks.

OkHttp supports Android 2.3 and above.

Add the below dependency to your android Project.

compile 'com.squareup.okhttp:okhttp:2.4.0'
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
0

According to Google Docs:

To continue using the Apache HTTP client, apps that target Android 9 and above can add the following to their AndroidManifest.xml:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

This works for me - SDK level 28.

Satish Shetty
  • 303
  • 2
  • 10