Questions tagged [okhttp]

An HTTP+HTTP/2 client for Android and Java applications.

OkHttp

An HTTP & SPDY client for Android and Java applications. For more information see the website and the wiki.

Overview

HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.

OkHttp is an HTTP client that’s efficient by default:

  • HTTP/2 and SPDY support allows all requests to the same host to share a socket.
  • Connection pooling reduces request latency (if SPDY isn’t available).
  • Transparent GZIP shrinks download sizes.
  • Response caching avoids the network completely for repeat requests.

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. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers. OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.

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

You can try out OkHttp without rewriting your network code. The okhttp-urlconnection module implements the familiar java.net.HttpURLConnection API and the okhttp-apache module implements the Apache HttpClient API.

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

Contributing

If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.

When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. Please also make sure your code compiles by running mvn clean verify.

Before your code can be accepted into the project you must also sign the Individual Contributor License Agreement (CLA).

4446 questions
21
votes
6 answers

OkHTTP and Picasso don't run together

I use Picasso library in my project to load images ande cache them. It works good without any problem. However, when I try to use OkHttp library to perform data communication with my server (JSON communication), Picasso throws exceptions. I use the…
anL
  • 1,073
  • 1
  • 11
  • 31
20
votes
5 answers

CertPathValidatorException connecting to a Let's Encrypt host on Android M or earlier

[edit: If you are here for Let's Encrypt expiry event from January 2021, read this first https://letsencrypt.org/2020/12/21/extending-android-compatibility.html] Connecting to https://valid-isrgrootx1.letsencrypt.org/ via OkHttp on Android M or…
Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
20
votes
4 answers

How to replace deprecated okhttp.RequestBody.create()

I try to upload an image from an Android App to a Django server using Retrofit 2 and OkHttp3. For that, I used to create a RequestBody instance using the following lines: RequestBody requestImageFile = // NOW this call is…
ebeninki
  • 909
  • 1
  • 12
  • 34
20
votes
4 answers

mocking Retrofit response calls with Call not working

I'm mocking the response of the APIService. Unfortunately it is not working, I have to send back a Call but I don't understand how. The question is how to send back a Call object. @RunWith(AndroidJUnit4::class) class ApiServiceTest { @Test …
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
20
votes
2 answers

OkHttpClient limit number of connections?

Is it possible with OkHttpClient to limit the number of live connections? So if limit reached, no new connection is picked and established? My app starts many connection at same time.
János
  • 32,867
  • 38
  • 193
  • 353
20
votes
5 answers

Detect if OkHttp response comes from cache (with Retrofit)

Is there a way to detect if a Retrofit response comes from the configured OkHttp cache or is a live response? Client definition: Cache cache = new Cache(getCacheDirectory(context), 1024 * 1024 * 10); OkHttpClient okHttpClient = new…
whlk
  • 15,487
  • 13
  • 66
  • 96
20
votes
1 answer

How to resolve java.lang.AssertionError when creating OkHttpClient in mockito?

I'm trying to make some canned network respones. I have the json response for the actual request and I have Retrofit interfaces that serialize responses. I am beyond frustrated trying to set this up. What should I be doing here? It seems my options…
Tyler Pfaff
  • 4,900
  • 9
  • 47
  • 62
20
votes
1 answer

Retrofit 2: How to set individual timeouts on specific requests?

I have set a global timeout in my Retrofit adapter by doing OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setReadTimeout(20, TimeUnit.SECONDS); okHttpClient.setConnectTimeout(20, TimeUnit.SECONDS); retrofit = new…
Ersen Osman
  • 7,067
  • 8
  • 47
  • 80
20
votes
1 answer

Read and Write timeouts behavior

What is the behavior of read and write timeouts in OkHttp? Is the timeout exception triggered when the whole request exceeds the timeout duration or is when the socket doesn't receive (read) or send (write) any packet for this duration. I think is…
Alex Miragall
  • 203
  • 2
  • 4
20
votes
2 answers

How to configure the Http Cache when using Volley with OkHttp?

I want to try Volley combining with OkHttp but Volley cache system and OkHttp both rely on the HTTP cache as defined in the HTTP specification. So how can be disabled the cache of OkHttp for keeping one copy of HTTP cache? EDIT: what I have…
Xiaozou
  • 1,655
  • 1
  • 15
  • 29
20
votes
1 answer

Retrofit - Okhttp client How to cache the response

I'm trying to cache the response of http calls done by Retrofit(v 1.9.0) with OkHttp(2.3.0). It always made the network calls if I try to make a call without internet then java.net.UnknownHostException. RestClient public class RestClient { public…
ImMathan
  • 3,911
  • 4
  • 29
  • 45
20
votes
1 answer

Why should I use OkHttp instead of android httpClient and AsyncTask

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…
RCB
  • 2,253
  • 2
  • 25
  • 49
19
votes
2 answers

UnknownHostException after Android 11 update

Problem: Once an UnknownHostException is returned, the user continues to receive the same error unless the app is reinstalled or the device is rebooted. Of the users whose OS is Android 11, only a few users are having problems. The biggest problem…
hanmolee
  • 253
  • 3
  • 9
19
votes
1 answer

Getting an error Using 'body(): ResponseBody?' is an error. moved to val with okhttp

Using response.body() gives me an error of "Using 'body(): ResponseBody?' is an error. moved to val" i tried removing ? but nothing works the error is in body() override fun onResponse(call: Call, response: Response) { val body =…
CHALA19X
  • 311
  • 2
  • 7
19
votes
4 answers

ProtocolException: Expected ':status' header not present

Retrofit network calls fails with a Protocol Exception suddenly in a working app. The app was working till yesterday and today all the network calls fails. The calls works fine with HTTP but fails with HTTPS. Here is the…