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
2
votes
0 answers

How to set a delay for interceptor in okhttp?

Lets say we need to retry a request in case of exception: public class TestUpInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { final Response response =…
Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
2
votes
0 answers

Retrofit 2 + interceptors at the level of Call

I'm evaluating Retrofit 2 for our project (Java-based Server Side application with microservices architecture, microservices interact via HTTP) and have a hard time to understand the following: Documentation of OkHttpClient states that instance of…
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
2
votes
1 answer

OkHttp connection leaks due to cloned client

I'm working on an issue related to Okhttp connection leaks. The leak stack trace seems to pointing to this WebSocket connection being opened in PodOperationsImpl#L267: try { URL url = new…
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
2
votes
1 answer

Requests not being made from android app, with max-age set

I have an api that returns an object as the response, and an etag and max age as headers. The response looks like this: HEADERS: 'x-frame-options': 'SAMEORIGIN', 'x-xss-protection': '1; mode=block', 'x-content-type-options': 'nosniff', …
Lazarus Rising
  • 2,597
  • 7
  • 33
  • 58
2
votes
1 answer

Curl to okhttp conversion

I have a curl of the form curl -v -u username:password -H "Content-Type: application/json"........ Not able to crack how to get the -u part working. Have tried multiple options like Authenticator proxyAuthenticator = new Authenticator() { …
Ananth
  • 63
  • 9
2
votes
1 answer

OkHttp3 Class not found Exception but is present in apk's dex file

I am building an android library that uses okhttp version 3.10.0 and it works so far. No compiler issues, functioning requests in the test app and everything is fine. As soon as i want to include the built aar file in another app i get a…
dedda1994
  • 151
  • 1
  • 15
2
votes
0 answers

Android : HTTPS urls are not working in Okhttp3

I am using Okhttp3 in my android application to download files. I am having problem with https urls. I have two URLS String url1 = "https://cbsenet.nic.in/cbsenet/PDFDEC2014/Paper%20III/D-01-3.pdf"; String url2 =…
hard coder
  • 5,449
  • 6
  • 36
  • 61
2
votes
3 answers

Testing OkHttp with MockWebServer and MockResponse with a Buffer body

I am trying to test some download code involving OkHttp3 and failing miserably. Goal: test downloading an image file and verify it worked. Platform: Android. This code is working in production but the test code just isn't making any sense. Prod…
AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
2
votes
1 answer

okhttp cache:how to get cache data first then get network data in network connetting?

ref:https://github.com/square/okhttp/issues/4003 Most of the solutions are network request networks, no net request cache. My requirement is to have a net first request the cache and then to request the network. thx!!!! I am trying like this: …
wgyscsf
  • 41
  • 4
2
votes
0 answers

Not able to throw exception from onFailure of the okhttp callback

When I try to throw to exception from onFailure method Android Studio forces me to add try catch block and not allowing to throw exception to calling function. protected void createUser(final UserRegister user) throws ApiException, RuntimeException…
Nikhil
  • 91
  • 1
  • 7
2
votes
0 answers

java.io.IOException: unexpected end of stream on Connection

I am getting this issue with Retrofit but with postman it is working fine. java.io.IOException: unexpected end of stream on Connection{2.test.esb.demoapp.com:80, proxy=DIRECT hostAddress=2.test.esb.demoapp.com/172.16.33.156:80…
Harshad07
  • 608
  • 7
  • 23
2
votes
1 answer

How do I do multiplexing on OkHttp?

I see some older questions about using SPDY (though the code has changed significantly since then), but no insights into how to properly leverage multiplexing and/or pipelining in OkHttp. I've gone through all the examples and have yet to see…
darkfrog
  • 1,053
  • 8
  • 29
2
votes
2 answers

Download multiple concurrent files

I need download multiple files in one time (About 100 files) It does not matter whether the download is synchronized And the important thing is that all files be downloaded. My code for getting urls and file names: for (int i = 0; i <…
AhmadReza
  • 421
  • 6
  • 20
2
votes
0 answers

SSLHandshakeException on WebSocket connection with OkHttp3

I'm trying to connect to WSS websocket using OkHttp3, but I got in onFailure() method of my WebSocketListener in particular the Exception said: javax.net.ssl.SSLHandshakeException: Handshake failed Now, I'm using this code to connect to…
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
2
votes
2 answers

is not public in 'retrofit2.Retrofit' , cannot be accessed from outside

im new in retrofit and im using retrofit2 version 2.4.0 . there is my Api client code : public class ApiClient { public static final String BASE_URL = "https://api.coinmarketcap.com/v1/"; public static Retrofit retrofit = null; public static…
Hadi Ahmadi
  • 129
  • 12