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
371
votes
22 answers

Logging with Retrofit 2

I'm trying to get the exact JSON that is being sent in the request. Here is my code: OkHttpClient client = new OkHttpClient(); client.interceptors().add(new Interceptor(){ @Override public com.squareup.okhttp.Response intercept(Chain chain)…
Gabor
  • 7,352
  • 4
  • 35
  • 56
261
votes
11 answers

How to make "inappropriate blocking method call" appropriate?

I am currently trying to leverage kotlin coroutines more. But I face a problem: when using moshi or okhttp inside these coroutines I get a warning: "inappropriate blocking method call" What is the best way to fix these? I really do not want to be…
ligi
  • 39,001
  • 44
  • 144
  • 244
210
votes
11 answers

How to set connection timeout with OkHttp

I am developing app using OkHttp library and my trouble is I cannot find how to set connection timeout and socket timeout. OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Response response =…
kelvincer
  • 5,929
  • 6
  • 27
  • 32
158
votes
5 answers

why use Retrofit when we have OkHttp

With OkHttp we can make HTTP request then get response from server: OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); Then with Gson…
Mehrdad Faraji
  • 3,744
  • 3
  • 25
  • 38
158
votes
7 answers

Can Retrofit with OKHttp use cache data when offline

I'm trying to use Retrofit & OKHttp to cache HTTP responses. I followed this gist and, ended up with this code: File httpCacheDirectory = new File(context.getCacheDir(), "responses"); HttpResponseCache httpResponseCache = null; try { …
osrl
  • 8,168
  • 8
  • 36
  • 57
150
votes
8 answers

Trusting all certificates with okHttp

For testing purposes, I'm trying to add a socket factory to my okHttp client that trusts everything while a proxy is set. This has been done many times over, but my implementation of a trusting socket factory seems to be missing something: class…
seato
  • 2,061
  • 2
  • 15
  • 18
124
votes
11 answers

How to add headers to OkHttp request interceptor?

I have this interceptor that i add to my OkHttp client: public class RequestTokenInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); // Here where we'll…
Morteza Rastgoo
  • 6,772
  • 7
  • 40
  • 61
120
votes
6 answers

Retrofit2 Authorization - Global Interceptor for access token

I'm trying to use Retrofit2, I want to add Token to my Header Like this: Authorization: Bearer Token but the code below doesn't work: public interface APIService { @Headers({"Authorization", "Bearer "+ token}) …
farshad
  • 1,339
  • 2
  • 9
  • 14
115
votes
9 answers

Okhttp3 - RequestBody.create(contentType, content) Deprecated

I did not find any example of how to replace the deprecation method. The examples on the okhttp3 main page are old. This is one of them: public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); OkHttpClient client =…
Matías
  • 1,200
  • 2
  • 7
  • 8
112
votes
10 answers

Can't get OkHttp's response.body.toString() to return a string

I'm trying to get some json data using OkHttp and can't figure out why when i try logging the response.body().toString() what i get is Results:﹕ com.squareup.okhttp.Call$RealResponseBody@41c16aa8 try { URL url = new URL(BaseUrl); …
freddieptf
  • 2,134
  • 2
  • 15
  • 16
110
votes
15 answers

How to use OKHTTP to make a post request?

I read some examples which are posting jsons to the server. some one says : OkHttp is an implementation of the HttpUrlConnection interface provided by Java. It provides an input stream for writing content and doesn't know (or care) about what…
user2219372
  • 2,385
  • 5
  • 23
  • 26
97
votes
8 answers

OkHttp how to log request body

I'm using an interceptor, and I would like to log the body of a request I'm making but I can't see any way of doing this. Is it possible ? public class LoggingInterceptor implements Interceptor { @Override public Response intercept(Chain…
Mathieu de Brito
  • 2,536
  • 2
  • 26
  • 30
96
votes
15 answers

How to retry HTTP requests with OkHttp/Retrofit?

I am using Retrofit/OkHttp (1.6) in my Android project. I don't find any request retry mechanism built-in to either of them. On searching more, I read OkHttp seems to have silent-retries. I don't see that happening on any of my connections (HTTP or…
dev
  • 11,071
  • 22
  • 74
  • 122
88
votes
10 answers

Does OkHttp support accepting self-signed SSL certs?

I'm working for a customer who has a server with self-signed SSL cert. I'm using Retrofit + CustomClient using wrapped OkHttp client: RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Config.BASE_URL + Config.API_VERSION) …
cesards
  • 15,882
  • 11
  • 70
  • 65
87
votes
4 answers

OkHttp Post Body as JSON

So, back when I was using Koush's Ion, I was able to add a json body to my posts with a simple .setJsonObjectBody(json).asJsonObject() I'm moving over to OkHttp, and I really don't see a good way to do that. I'm getting error 400's all over the…
Pixel Perfect
  • 1,266
  • 1
  • 11
  • 14
1
2 3
99 100