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
30
votes
6 answers

Retrofit - Too many follow-up requests: 21

I'm using retrofit to make requests. I've got following error: java.net.ProtocolException: Too many follow-up requests: 21 The code is like below: private OkHttpClient httpClient; private CookieManager cookieManager; public S…
Marcin Bortel
  • 1,190
  • 2
  • 14
  • 28
29
votes
1 answer

Does Android use OkHttp internally?

This is a stack trace I get when running an Android app I recently inherited. We're not using OkHttp as an explicitly dependency, and the com.android.okhttp in the trace makes me think the AOSP is using OkHttp now internally? java.lang.Throwable:…
user3837683
  • 325
  • 1
  • 3
  • 6
29
votes
9 answers

OkHttp - Enable logs

I used Retrofit in order to make HTTP requests and JSON parsing and I loved the way to turn on debug logs. Logs allow to see body requests, URL... which is very useful. As Retrofit use OkHttp, I'm wondering if OkHttp also have a way to enable logs…
Jul
  • 1,039
  • 3
  • 12
  • 20
28
votes
0 answers

HTTP/2 protocol not working with okhttp

I am using Retrofit 1.9 with okhttp 2.4.0. So far we have SPDY protocol disabled on server side (I checked it by this ). And enabled protocol on server side is HTTP/2 (I checked it by this). So I was thinking that okhttp will try to make an api call…
Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
28
votes
2 answers

Now that SSLSocketFactory is deprecated on Android, what would be the best way to handle Client Certificate Authentication?

I am working on an Android app that requires Client Certificate Authentication (with PKCS 12 files). Following the deprecation of all that's apache.http.*, we have started a pretty big work of refactoring on our network layer, and we have decided…
TheYann
  • 1,367
  • 2
  • 12
  • 13
27
votes
2 answers

Retrofit crashes when I throw an exception in an interceptor

I have an interceptor that handles some authentication things. If the authentication fails it throws and Exception. According to what I could find throwing an exception should result in onFailure getting called where I handle the exception.…
Djangow
  • 341
  • 3
  • 9
27
votes
2 answers

How do I post data using okhttp library with content type x-www-form-urlencoded?

I have used this method https://stackoverflow.com/a/31744565/5829906 but doesnt post data. Here is my code OkHttpClient client = new OkHttpClient(); RequestBody requestBody = new MultipartBuilder() …
MrRobot9
  • 2,402
  • 4
  • 31
  • 68
27
votes
6 answers

Okhttp Authenticator multithreading

I am using OkHttp in my android application with several async requests. All requests require a token to be sent with the header. Sometimes I need to refresh the token using a RefreshToken, so I decided to use OkHttp's Authenticator class. What will…
hexonxons
  • 845
  • 1
  • 10
  • 19
27
votes
2 answers

okhttp application level OkHttpClient instance

I was wondering if there will be any performance bottleneck or issues if I create one instance of OkHttpClient to serve my "entire android application". I.e.In my Application class, I create a static public variable that will contain a instance of…
Mutinda Boniface
  • 518
  • 1
  • 5
  • 14
26
votes
6 answers

How to change body in OkHttp Response?

I'm using retrofit. To catch response i'm using Interceptor: OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.interceptors().add(myinterceptor); here is code of interceptor: new Interceptor() { @Override public Response…
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
26
votes
4 answers

How to download image file by using okhttpclient in Java

I would like ask how to download image file by using okhttpclient in Java since I need to download the file with session. here is the code given officially, but I don't know how to use it for downloading as image file. private final OkHttpClient…
user3338304
  • 291
  • 2
  • 4
  • 5
25
votes
6 answers

OKHttp throwing an illegal state exception when I try to log the network response

I put the following interceptor on my OkHttp client: httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response response = chain.proceed(chain.request()); …
25
votes
4 answers

How to send post parameters dynamically (or in loop) in OKHTTP 3.x in android?

I am using OKHTTP 3.x version. I want to post multiple parameters and would like to add the params in a loop. I know that in version 2.x , I can use FormEncodingBuilder and add params to it in loop and then from it create a request body. But In 3.x…
intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
25
votes
9 answers

ProGuard not working with okhttp

ProGuard won't play nice with okhttp and I keep getting the following Warnings: Warning:com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl: can't find referenced method 'long getContentLengthLong()' in program class…
wkarl
  • 781
  • 1
  • 8
  • 19
25
votes
3 answers

Square`s OkHttp. Download progress

Is there any way to get downloading file progress when using square`s OkHttp? I did not find any solution in Recipes. They have class Call which can get content asynchronically, but there is no method to get current progress.
Kalyaganov Alexey
  • 1,701
  • 1
  • 16
  • 23