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
25
votes
5 answers

Retrofit POST request w/ Basic HTTP Authentication: "Cannot retry streamed HTTP body"

I'm using Retrofit to do a basic POST request, and I'm providing a basic @Body for the request. @POST("/rest/v1/auth/login") LoginResponse login(@Body LoginRequest loginRequest); When I'm building the interface for Retrofit I'm providing my own…
spierce7
  • 14,797
  • 13
  • 65
  • 106
24
votes
3 answers

Do we have any possibility to stop request in OkHttp Interceptor?

In our app we met with one special case - if our App.specialFlag == true, we need stop any request from our code. And we think, that the best approach in this situation is include special Interceptor which will stop any our requests, something like…
Pavel Strelchenko
  • 536
  • 1
  • 6
  • 19
24
votes
3 answers

javax.net.ssl.SSLPeerUnverifiedException: Hostname not verified:

I am trying to use HTTPS connection with self-signed certificate. I have followed steps of creating self-signed certificate as mentioned here - Creating Self-signed certificate. Everything works fine even in browser, it only shows me a message that…
CROSP
  • 4,499
  • 4
  • 38
  • 89
24
votes
2 answers

How cancel task with retrofit and rxjava

I have rest api. @Get("/serveraction") public Observable myRequest(@Query("Data") String data); I know, that okhttp has canceling functionality(by request object, by tag), but don't know how use it with retrofit and rxjava. What is the best…
zella
  • 4,645
  • 6
  • 35
  • 60
24
votes
3 answers

Upload binary file with okhttp from resources

I need to upload a binary file bundled in an apk to a server using okhttp. Using urlconnection, you can simply get an inputstream to an asset and then put that into your request. However, okhttp only gives you the option of uploading byte arrays,…
dabluck
  • 1,641
  • 2
  • 13
  • 20
24
votes
2 answers

How is OkHttp performing parallell HTTP Requests with seemingly synchronous HTTP connections, without the use of threading?

I've done some performance testing with the OkHttp library and found it to be great. It did 80 requests to http://httpbin.org/delay/1, which deliberately pauses 1 sec for each request, in 4.7s on my HTC One phone. I've looked at the code and I'm…
Nilzor
  • 18,082
  • 22
  • 100
  • 167
23
votes
4 answers

How to resolve D8 warning: D8: Type `org.conscrypt.Conscrypt` was not found

/Volumes/SSD/.gradle/caches/modules-2/files-2.1/com.squareup.okhttp3/okhttp/3.10.0/7ef0f1d95bf4c0b3ba30bbae25e0e562b05cf75e/okhttp-3.10.0.jar: D8: Type `org.conscrypt.Conscrypt` was not found, it is required for default or static interface methods…
smoothdvd
  • 2,389
  • 4
  • 20
  • 25
23
votes
3 answers

Firebase isn't reporting crashes related to OkHttp

I was migrating my code from Google analytics to Firebase, following problem I'm facing Some of the custom events show correct value while others not although code used are same in all cases. Can provide code if required. Update : Above is solved,…
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
23
votes
4 answers

Image upload using okHttp

i want to upload image using okhttp but i am not able to find MultipartBuilder for Post Image.What can i use instead of this. Here is my code public static JSONObject uploadImage(File file) { try { final MediaType MEDIA_TYPE_PNG =…
Dhaval
  • 2,724
  • 2
  • 24
  • 33
23
votes
3 answers

how to solve javax.net.ssl.SSLPeerUnverifiedException: Hostname .com not verified:

I am having the following error Hostname domain.com not verified: Not "javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated" I was able before to connect to different host, in this host I am facing problems , how to fix it …
Moudiz
  • 7,211
  • 22
  • 78
  • 156
23
votes
3 answers

Returning error from OKHttp interceptor (using retrofit)

I am using OkHttp with Retrofit to make my app's network requests. I am also using Interceptors for Authentication and retrying requests if necessary. The server sometimes has temporary problems, and returns an empty body although the response…
Murat Ögat
  • 1,342
  • 2
  • 14
  • 24
23
votes
1 answer

How to convert the response from Okhttp into inputstream

CODE: public static String getRequestNoPayload(String urlString, String loginApi, String mUsername) throws Exception { Response response; try{ client.setConnectTimeout(20, TimeUnit.SECONDS); // connect timeout …
Devrath
  • 42,072
  • 54
  • 195
  • 297
22
votes
2 answers

How to set MockWebServer port to WebClient in JUnit test?

I'm using spring-boot with WebClient, which is autowired as a bean. Problem: when writing a junit integration test, I have to use okhttp MockWebServer. This mock always starts up on a random port, eg localhost:14321. Now my WebClient of course has a…
membersound
  • 81,582
  • 193
  • 585
  • 1,120
22
votes
7 answers

FATAL EXCEPTION: OkHttp Dispatcher

I'm using the OkHttp library in my android app to make web requests to a weather API. I've already implemented my code and I'm getting a FATAL EXCEPTION when doing the request. I've already added INTERNET permissions in my manifest…
fishhau
  • 459
  • 1
  • 3
  • 15
22
votes
3 answers

OKhttp PUT example

My requirement is to use PUT, send a header and a body to server which will update something in the database. I just read okHttp documentation and I was trying to use their POST example but it doesn't work for my use case (I think it might be…
user5812467