Why should I use OkHttp instead of android httpClient and AsyncTask

RCB picture RCB · Sep 9, 2014 · Viewed 13.6k times · Source

In the presentation of Paresh Mayani at SpeakerDeck (https://speakerdeck.com/pareshmayani/lazy-android-developers-be-productive) he says that it's better to use OkHttp or Retrofit instead of AsyncTask with DefaultHttpClient.

My question is why?
Why are they faster?
Aren't those also libraries based on the default android classes?
What is the difference between OkHttp and Retrofit ?

Answer

chickenbane picture chickenbane · Sep 19, 2014

As always, engineering is about balancing the trade-offs to build the best solution for your requirements.

Using a library instead of the platform is a good example. The API of the platform has existed for quite some time, and for compatibility reasons the Android team has less flexibility in changing those interfaces. A library doesn't have those constraints; for example, if the host supports it OkHttp can use the SPDY protocol for lower latency, compression, multiplexing, etc., which can make your Android app more responsive.

OkHttp and Retrofit - which are projects from Square - can work together. They are also regular Java libraries, so they do not depend on / based on Android. OkHttp handles the lower-level HTTP connection details, while Retrofit simplifies using REST APIs. Retrofit can be used on top of OkHttp, but it is not required.

Check out the projects websites, they are also hosted on github.

http://square.github.io/okhttp/

http://square.github.io/retrofit/