Difference between okhttp and httpurlconnection?

What is the difference between these two libraries?

As I understand it, there is a difference between the two libs also because Volley uses httpurlconnection and redid okhttp ....

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

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

EDIT:

Why does android use okhttp for httpurlconnection? before httpurlconnection didn't use okhttp if i'm not mistaken

+10
source share
3 answers

Pros okHttp OkHttp can be configured for each request easily - for example, setting a timeout, etc. For each request. OkHttp is saved when the network is a concern: it will quietly recover due to common connection problems. If your service has multiple IP addresses, OkHttp will try to use alternative addresses if the first connection fails.

Full analytics of any request can be obtained. You can find out the transmitted bytes, the received bytes and the time spent on any request. These analytics are important so that you can find the use of your application data and the time taken for each request so that you can identify slow requests.

Using OkHttp is very simple. Its request / response API is designed with smooth builders and immutability. It supports both synchronous blocking calls and asynchronous calls with callbacks.

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

+8
source

The APIs are different, I personally prefer OkHttp.

Please note that starting with Android 4.4, the network layer (just like the HttpUrlConnection API) is implemented through OkHttp.

+2
source

HttpURLConnection

Benefits:

  • Lightweight apis helps in easier handling and reduces compatibility issues.
  • Automatic processing of caching mechanisms using HttpResponseCache.
  • Reduces network usage as well as battery consumption.

Request 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

Benefits:

  • Connection pool
  • Gziping
  • Caching
  • Recovering from network problems
  • Redirection
  • Repetitions
  • Support for synchronous and asynchronous calls

Request 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(); 
0
source

Source: https://habr.com/ru/post/1262581/


All Articles