Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between okhttp and httpurlconnection?

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

like image 951
user155293 Avatar asked Jan 10 '17 14:01

user155293


3 Answers

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.

like image 133
Shailendra Kushwah Avatar answered Nov 13 '22 09:11

Shailendra Kushwah


HttpURLConnection

Advantages:

  • Lightweight APIs help in easier management and reduces compatibility issues.
  • Automatic handling of the caching mechanisms, with the help of HttpResponseCache.
  • Reduces the network usage and also, the battery consumption.

Query Parameter:

    URI baseUri = new URI("www.exemple.com/search");
URI uri = applyParameters(baseUri, "word","java");
HttpURLConnection connection = 
    (HttpURLConnection) uri.toURL().openConnection();
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setRequestMethod("GET");
connection.connect();
if (connection.getResponseCode() == 
   HttpURLConnection.HTTP_OK) {
   // ...
}

Android Headers Example:

conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("CustomHeader", token);

OkHttp

Advantages:

  • Connection pooling
  • Gziping
  • Caching
  • Recovering from network problems
  • Redirects
  • Retries
  • Support for synchronous and asynchronous calls

Query Parameter:

HttpUrl.Builder urlBuilder = HttpUrl.parse("https://httpbin.org/get").newBuilder();
urlBuilder.addQueryParameter("website", "www.journaldev.com");
urlBuilder.addQueryParameter("tutorials", "android");
String url = urlBuilder.build().toString();

Request request = new Request.Builder()
                     .url(url)
                     .build();

Android Headers Example:

Request request = new Request.Builder()
    .header("Authorization", "replace this text with your token")
    .url("your api url")
    .build();
like image 45
HeinerTheBest Avatar answered Nov 13 '22 07:11

HeinerTheBest


The API's are different, personally I prefer the OkHttp ones.

Note that starting from Android 4.4, the networking layer (so also the HttpUrlConnection APIs) is implemented through OkHttp.

like image 3
Marco Righini Avatar answered Nov 13 '22 07:11

Marco Righini