Difference between okhttp and httpurlconnection?

user155293 picture user155293 · Jan 10, 2017 · Viewed 10.6k times · Source

What are the differences between these 2 libraries?

How I understood there is a difference between these 2 lib also because Volley uses httpurlconnection and Retrofit the okhttp....

But I don't understand the difference between them and pros and cons of both solutions. When is okhttp better and when httpurlconnection?

I would like to know so I know when should I use them.

EDIT:

Why does android use okhttp for the httpurlconnection? before httpurlconnection was not using okhttp if I am not wrong

Answer

Shailendra Kushwah picture Shailendra Kushwah · Jan 10, 2017

Pros of okHttp OkHttp can be customized for every request easily — like timeout customization, etc. for each request. OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails.

Complete analytics of any request can be obtained. You can know bytes sent, bytes received, and the time taken on any request. These analytics are important so that you can find the data usage of your application and the time taken for each request, so you can identify slow requests.

Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.

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