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

Retrofit java.net.ProtocolException: Expected ':status' header not present

Retrofit network calls fails with a Protocol Exception suddenly in a working app. The app was working till yesterday and today all the network calls fails. The calls works fine with HTTP and with some HTTPS except my production endpoint. It seems…
Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
2
votes
0 answers

Android Receiving Messages From WebSocket Server

I am working on an Android application where I need to connect to a web socket and receive messages to unlock the app from the web server. Receiving messages have been implemented through Stompclient on java script but i need to do the same on…
2
votes
1 answer

Android okhttp: Getting randomly java.net.ProtocolException: Unexpected status line: 1.1 200 OK

when using androids URL and HttpUrlConnection to send a GET request to a backend point, it sometimes (1 out of 10) occurs, that the request fails due to: java.net.ProtocolException: Unexpected status line: 1.1 200 OK As said this only happens…
2
votes
1 answer

How to Create Custom Empty okhttp3.Response?

PROBLEM: Method returns Response, sometimes null, if request call fails. Error message from exception should be added to returned Response manually. RESEARCH: Request request; Response response = null; try { response = client …
Zon
  • 18,610
  • 7
  • 91
  • 99
2
votes
1 answer

Retrofit OAuth signing with okhttp-signpost get OAuthMessageSignerException for special character

I am using Retrofit 2 to make http request in Android App. The server I talk to require OAuth 1.0 Authorization. I use okhttp-signpost from here to handle OAuth signing. Here's my build.gradle for library included: compile…
Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42
2
votes
0 answers

Socket Timeout Exception In Okhttp3Client

In work, I encounter a problem like this. I used Rest-Template embedded in Spring. HttpClient used is OkHttp3Client. In On-line running, the error like below happens once an hour.It seems like socket is closed,but OkHttp3Client uses long connection…
2
votes
0 answers

Okhttp FormBody add Image Base64 string takes hundreds of millisecond

Here is the code that I wrote. When I use FormBody.Builder to add the picture's Base64 string, it takes hundreds of milliseconds. I have no idea to solve this problem. ByteArrayOutputStream baos = new ByteArrayOutputStream(); Bitmap cropBmp =…
W.Marshall
  • 21
  • 2
2
votes
1 answer

OkHttp Per Request Timeout Settting?

Click Here To show my issue,I just want set per request timeout and other paramters, so just use: private static final OkHttpClient globalOkHttpClient = new OkHttpClient.Builder().build(); private static final…
liliang
  • 21
  • 4
2
votes
2 answers

How to catch okhttp3 WebSocket network activity using okhttp3.Interceptor?

I have an okhttp3 (3.9.1) WebSocket instance and would like to view all it's network requests and responses. I tried to add some okhttp3.Interceptor instances to OkHttpClient instance before creating WebSocket on it but had no luck in viewing…
2
votes
2 answers

Android Retrofit Upload File with authorization Body

I'm using retrofit to connect to server which needs authorization token in POST Data. Example For the request: https://adress/inboxes POST data: token=jasdf807gb123uy40bviubva08sdyv123&message_id=1& I use method like this and its works…
Big Coach
  • 2,485
  • 2
  • 12
  • 31
2
votes
1 answer

javax.net.ssl.SSLPeerUnverifiedException: Hostname XXX not verified, for no self-signed cert

Problem Recently I noticed that 1-0.5% of all users faced with javax.net.ssl.SSLPeerUnverifiedException during simple GET over HTTPS. But they looks interesting: exception message contains certificates information and according to this information…
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
2
votes
0 answers

OkHttp returns wrong response code and body after failed connection attempt

I'm getting successful response code and wrong response body which corresponds to previous request, when I am expecting Bad request (400) response code and error response body. This happens every time when request call is enqueued after failed…
Ana
  • 73
  • 7
2
votes
0 answers

Upload large files to server using okhttp3

I am trying to upload all type of file to the server using okhttp3. I successfully uploaded files less than 2MB but for large files, uploading stops mid where. Code is public void uploadfile(String filePath) { File f = new File(filePath); …
Abhishek c
  • 539
  • 6
  • 16
2
votes
2 answers

How to add ArrayList of Integers to MultipartBody

I have an ArrayList of Integers that I need to put into my MultipartBody.Builder. I have tried using different methods of .addFormDataPart, even tried to convert the arraylist to a JSONObject and then the JSONObject to a string, but my server would…
Torched90
  • 305
  • 1
  • 3
  • 18
2
votes
1 answer

Custom HTTP method not working in Retrofit 2

Hello I have used custom method in my code as below but it always gives me java.lang.IllegalArgumentException: method AUTH must not have a request body. My code is not working it always says: Custom method AUTH, must not have a…
Chetak Bhimani
  • 459
  • 4
  • 19