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
19
votes
3 answers

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…
user155293
  • 373
  • 1
  • 3
  • 14
19
votes
5 answers

Does OkHttpClient have a max retry count

I am setting the retry on connection failure option for OkHttpClient. client = new OkHttpClient(); client.setRetryOnConnectionFailure(true); I will like to know how many times it will keep trying. Looking at the source code I did not see any…
RajV
  • 6,860
  • 8
  • 44
  • 62
19
votes
1 answer

OkHTTPClient Proxy authentication how to?

Question: How do I add a authorization proxy to OkHTTP. I know that OkHTTP's builder does support proxies although I am having a hard time setting one up. /** * Given a Url and a base64 encoded password return the contents of a website. * @param…
Liably
  • 327
  • 1
  • 2
  • 14
19
votes
2 answers

Getting Request body content using Retrofit 2.0 POST method

I have a requirement to get a request body and to perform some logic operations with Retrofit 2.0 before doing enque operation. But unfortunately I am not able to get the post body content from my service call. At present after searching a lot I…
Chandru
  • 5,954
  • 11
  • 45
  • 85
19
votes
5 answers

HTTP Caching with Retrofit 2.0.x

I'm trying to cache some responses in my app using Retrofit 2.0, but I'm missing something. I installed a caching file as follows: private static File httpCacheDir; private static Cache cache; try { httpCacheDir = new…
Amr Barakat
  • 686
  • 1
  • 7
  • 16
19
votes
4 answers

How to access context in a Interceptor?

I would like to save some stuff on the SharedPreferences while being on a Interceptor. I can't find a way to do it because i can't find a way to access the context on the Interceptor (so not possible to use PreferencesManager, etc). public class…
se0
  • 201
  • 1
  • 2
  • 4
19
votes
1 answer

When to call response.body().close()

I have an Android SDK that uses OkHttp. In my sample app that uses my SDK everything works fine. One of my users however, when StrictMode is turned on, is getting the following java.lang.Throwable: Explicit termination method 'close' not called. I…
tbeauvais
  • 1,698
  • 4
  • 18
  • 22
19
votes
2 answers

Image adapter leaking memory

I have a simple ListActivity that shows images and I inizialize my OkHttpClient for Picasso Builder in the constructor of the ImageAdapter class: picassoClient = new OkHttpClient(); picassoClient.interceptors().add(new Interceptor() { @Override …
GPack
  • 2,494
  • 4
  • 19
  • 50
19
votes
1 answer

OkHttpClient close connection

I'd like to use OkHttpClient to load a url, and if the website at the given url responds with a pdf content type I will go ahead and download the PDF, otherwise I want to ignore the response. My question is, do I need to do anything special to…
Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43
19
votes
3 answers

Correct way of setting up cache for OkHttp in Android

I'm trying to set up the cache for OkHttp, so it only requests to the server the first time I try to retrieve a response from the server until the expires header date, or cache-control header that comes from the server invalidates the response from…
Adrián Pérez
  • 2,186
  • 4
  • 21
  • 33
19
votes
5 answers

How do you prevent Retrofit from automatically following a 302

I have an authentication call that i'm trying to make using Retrofit on Android. The call returns a 302 to either a success or failure page. The original 302 response brings back a session cookie needed to maintain authentication on success, however…
nak5ive
  • 1,963
  • 4
  • 15
  • 21
18
votes
4 answers

java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classe

I'm using retrofit (2.7.2) and OkHttp version (4.4.0) in an Android project and I'm facing a crash on a request. Dependencies: kapt("com.squareup.moshi:moshi-kotlin-codegen:1.9.2") implementation 'com.squareup.moshi:moshi:1.9.2' implementation…
A B
  • 235
  • 1
  • 2
  • 7
18
votes
3 answers

Mocking okhttp response

I have the below method which executes a GET request 3 times until its successfully. What would be the better way to mock this method? I want to mock and test status_code 401, status code 500 and want to test if the method is executed thrice. In…
user1050619
  • 19,822
  • 85
  • 237
  • 413
18
votes
2 answers

OkHttp SSLHandshakeException SSL handshake aborted Failure in SSL library, a protocol error

04-23 17:17:38.434 21599-21956/ D/NativeCrypto: ssl=0x0 NativeCrypto_SSL_interrupt 04-23 17:17:38.435 21599-21956/ D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted:…
Ankur
  • 677
  • 1
  • 7
  • 21
18
votes
2 answers

Make HttpLoggingInterceptor do not log images

I'm trying to get rid of trash in logs, like *�$ʞx���J/ when i recieve an image So i tried to override HttpLoggingInterceptor intercept(), to detect is there a Content-Type => image/jpeg header in responce, but HttpLoggingInterceptor is final so…
whereismaxmax
  • 496
  • 5
  • 17