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
86
votes
11 answers

Download binary file from OKHTTP

I am using OKHTTP client for networking in my android application. This example shows how to upload binary file. I would like to know how to get inputstream of binary file downloading with OKHTTP client. Here is the listing of the example : public…
pratsJ
  • 3,369
  • 3
  • 22
  • 41
78
votes
13 answers

Android OkHttp with Basic Authentication

I'm using the OkHttp library for a new project and am impressed with its ease of use. I now have a need to use Basic Authentication. Unfortunately, there is a dearth of working sample code. I'm seeking an example of how to pass username /…
Kerr
  • 993
  • 1
  • 7
  • 11
77
votes
6 answers

How does OkHttp get Json string?

Solution: It was a mistake on my side. The right way is response.body().string() other than response.body.toString() Im using Jetty servlet, the URL ishttp://172.16.10.126:8789/test/path/jsonpage, every time request this URL it will return…
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
76
votes
8 answers

javax.net.ssl.SSLException: Read error: ssl=0x9524b800: I/O error during system call, Connection reset by peer

Our clients are starting to see 100s of these "SSLException error - Connection reset by peer" over the last couple of weeks and I can't figure out why We're using Retrofit with okhttp, no special configuration public class OkHttpClientProvider…
Rickster
  • 1,393
  • 3
  • 13
  • 19
72
votes
9 answers

java.net.SocketTimeoutException: timeout

With OkHttp library, application is facing following SocketTimeoutException issue. If request size is less, then it is working fine(Less than 1MB). I am getting this exception within 10 seconds, even my socket timeout(readTimeout) value is much…
Vivek
  • 4,170
  • 6
  • 36
  • 50
68
votes
9 answers

CertPathValidatorException : Trust anchor for certificate path not found - Retrofit Android

I am creating an android application which uses https for communication with the server. I am using retrofit and OkHttp for making requests. These works fine for standard http requests. The following are the steps that I followed. Step 1 : Acquired…
Gowtham Raj
  • 2,915
  • 1
  • 24
  • 38
67
votes
4 answers

Expected Android API level 21+ but was 30

How is it possible that I get this message? It does not make any sense. I'm using com.squareup.retrofit2:retrofit:2.9.0 Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 30 at…
Oridadedles
  • 831
  • 1
  • 6
  • 8
65
votes
6 answers

Okhttp response callbacks on the main thread

I have created a helper class to handle all of my http calls in my app. It is a simple singleton wrapper for okhttp that looks like this (I have omitted some unimportant parts): public class HttpUtil { private OkHttpClient client; private…
Michael
  • 22,196
  • 33
  • 132
  • 187
63
votes
7 answers

Retrofit 2: Catch connection timeout exception

I have the following setup: final OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setReadTimeout(5, TimeUnit.SECONDS); okHttpClient.setConnectTimeout(5, TimeUnit.SECONDS); RestAdapter.Builder builder = new RestAdapter.Builder() …
Jdruwe
  • 3,450
  • 6
  • 36
  • 57
62
votes
5 answers

Chrome DevTools broken - Stetho unusable

Stetho and Google Chrome DevTools have become unusable after a macOS update (I doubt that macOS update is the origin of this bug, but I prefer to mention it). Here is the appearance of DevTools after the open of "inspect mode" on a device (provided…
61
votes
9 answers

How to add query parameters to a HTTP GET request by OkHttp?

I am using the latest okhttp version: okhttp-2.3.0.jar How to add query parameters to GET request in okhttp in java ? I found a related question about android, but no answer here!
Jerikc XIONG
  • 3,517
  • 5
  • 42
  • 71
61
votes
6 answers

Accessing body string of an OkHttp Response twice results in IllegalStateException: closed

I implement my http calls via the OkHttp library. Everything works fine, but I noticed that, when I access the body as a string of the response twice an IllegalStateException will be thrown. That is, I do (for example): Log.d("TAG",…
degill
  • 1,285
  • 2
  • 13
  • 19
61
votes
7 answers

How to specify a default user agent for okhttp 2.x requests

I am using okhttp 2.0 in my Android app and didn't find a way to set some common User Agent for all outgoing requests. I thought I could do something like OkHttpClient client = new OkHttpClient(); client.setDefaultUserAgent(...) ...but there's no…
dimsuz
  • 8,969
  • 8
  • 54
  • 88
59
votes
3 answers

Unsupported operation: Android, Retrofit, OkHttp. Adding interceptor in OkHttpClient

I am trying to add token based authentication via Retrofit 2.0-beta3 and OkHttpClient in Android using interceptors. But I get UnsupportedOperationException when I add my interceptor in OkHttpClient. Here is my code: In ApiClient.java public…
Usman khan
  • 841
  • 1
  • 9
  • 12
57
votes
12 answers

Automatic cookie handling with OkHttp 3

I am using okhttp 3.0.1. Every where I am getting example for cookie handling that is with okhttp2 OkHttpClient client = new OkHttpClient(); CookieManager cookieManager = new…
user2672763
  • 801
  • 1
  • 9
  • 18