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
1 answer

How to send Audio File Using Retrofit

I am Trying to send audio file using Retrofit but ResponseBody always null and Status is 500 internal server error ,I tried a lot of different things but nothing Works Postman Screenshots: body header My Client: public class AudioClient…
Ayat Khrisat
  • 130
  • 2
  • 15
2
votes
1 answer

RecyclerView returns empty

I've searched around and nothing seems to work. I have a RecyclerView object and it was running perfectly unless I tried to create a infinite scroll listener event. After that, it stopped working and returns an empty view. So I removed the code I…
Harman Kamboj
  • 429
  • 7
  • 20
2
votes
1 answer

Null object reference on API response after killing app. if app logout properly response will work

If app killed manually and try to sent update i am getting nullPointerException in API response. If I logout properly its working fine. I logged the input values everything is fine. anyone please look through this code This is my API update. …
2
votes
0 answers

Okhttp: get cookie from Set-cookie header in a series of redirects

I can use OkhttpClient to send a GET request to the server and get the cookie in the final response properly(the response code of which is 200 )and the cookie of which can be retrieved using addInterceptor(). Request-> 302(Set-cookie?) ->…
kolibreath
  • 125
  • 2
  • 13
2
votes
1 answer

okHttp3 response.code() won't return 304

I'm using OkHttp3 on an android app for listening to web services, keeping the question basic, the following is what does the trik: try(Response response = client.newCall(request).execute()) { responseBody = response.body().string(); …
Cliff Burton
  • 3,414
  • 20
  • 33
  • 47
2
votes
1 answer

Attach the SSL client cert okhttp3 . Handshake failed

I've been trying to get to add SSL connection with server and unfortunately it doesn't work - still getting javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.. The…
Ikazuchi
  • 433
  • 2
  • 12
2
votes
1 answer

How to correctly upload a video to server using okhttp client?

I am trying to upload a video to a server using okhttp client and although the onsuccess method is reached the video is not playable on the website. My question is, am I uploading the video correctly using the storage path or am I missing…
2
votes
1 answer

java.net.ProtocolException: unexpected end of stream happened when i uploaded a image to server by okhttp

The error happened when i upload a 115KB image file to server.(the most answer of stackoverflow is about download the file.I do not know if it is the same to those) the error information is below: onFailure : java.net.ProtocolException: unexpected…
李楷嘉
  • 21
  • 1
2
votes
1 answer

okhttp 3 very slow

I try to get json from a server, I use https on the server and every http request will go to the https version. I get the data and the data that I send works to but it takes up to 45 sec to get a response back. The same code was faster with the…
allegs34
  • 21
  • 1
  • 3
2
votes
0 answers

Connection Reset Exception on large file upload

I'm successfully uploading smaller files < 10MB via okhttp (3.8.1) via https. Testet on different Android 6 and 7 devices. buildtools 27.0.0 compiledSDK 27 Using following…
Ben
  • 117
  • 15
2
votes
1 answer

How to access cookie and check if it has expired using okhttp3 and PersistentCookieStore?

I am working on an Android app in which a log in post request is made to a webservice. The request returns a cookie which expires in 20 minutes. Using okhttp3 and this PersistentCookieStore library, I got the cookie to be stored and subsequently…
Ronald D
  • 21
  • 1
2
votes
0 answers

How to combine PersistentCookieStore with Webkit CookieManager?

I'm currently using PersistentCookieStore, as per shown in example here. https://stackoverflow.com/a/34886860/3286489 However, since I need to synchronized my cookies with webview, it looks like the PersistentCookieStore as it is, doesn't get sync…
Elye
  • 53,639
  • 54
  • 212
  • 474
2
votes
1 answer

Retrofit 2.3 Unable to find acceptable protocols on 8.0 devices

I'm trying to make it work on Oreo devices. This works perfectly on older devices but not on Oreo. I was having issues with handshake so I added ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) …
WinterChilly
  • 1,549
  • 3
  • 21
  • 34
2
votes
3 answers

Certificate pinning with okHttp

This question can be a duplicate of How can I pin a certificate with Square OKHTTP? But since it's not clear I'm asking again. I have to attach SSL certificate to my http client. I'm using retrofit version 2.2.0 and okHttp version 3.6.0 I have a…
user4260260
2
votes
0 answers

OkHttp POST error "connection reset by peer" for unauthorized call and large payload

I've been struggling with the following issue: I have a spring boot application which allows a user to post JSON content to an API endpoint. To use this endpoint, the user has to authenticate himself via basic authentication. Moreover, I use OkHttp…
Thylossus
  • 302
  • 1
  • 3
  • 11