Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use OkHttp instead of android httpClient and AsyncTask

Tags:

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 ?

like image 499
RCB Avatar asked Sep 09 '14 08:09

RCB


1 Answers

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/

like image 194
chickenbane Avatar answered Oct 03 '22 16:10

chickenbane