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

Is it thread-safe to make calls to OkHttpClient in parallel?

I have several threads that run at the same time and some of them need to request data from Internet. Do I need to care about synchronization of their access to OkHttpClient singleton? For example, Thread 1 ... Request request = new…
Dmitrii Demenev
  • 735
  • 1
  • 5
  • 13
43
votes
4 answers

Okhttp refresh expired token when multiple requests are sent to the server

I have a ViewPager and three webservice calls are made when ViewPager is loaded simultaneously. When first one returns 401, Authenticator is called and I refresh the token inside Authenticator, but remaining 2 requests are already sent to the…
sat
  • 40,138
  • 28
  • 93
  • 102
43
votes
3 answers

OkHttp: avoid leaked connection warning

I am using OkHttp 3, and I keep getting leaked connection warnings: WARNING: A connection to https://help.helpling.com/ was leaked. Did you forget to close a response body? Jul 14, 2016 6:57:09 PM okhttp3.ConnectionPool…
Alphaaa
  • 4,206
  • 8
  • 34
  • 43
43
votes
8 answers

Retrofit 2 example tutorial but GsonConverterFactory display error "Cannot resolve symbol"

I'm trying to follow Retrofit's 2 tutorial, but on this part of the code there is a GsonConverterFactory that displays error Cannot resolve symbol: public class ServiceGenerator { public static final String API_BASE_URL =…
StackOverflower
  • 480
  • 1
  • 4
  • 9
43
votes
4 answers

Does Retrofit make network calls on main thread?

I am trying to explore Retrofit+OkHttp on Android. Here's some code I found online : RestAdapter restAdapter = new RestAdapter.Builder().setExecutors(executor, executor) .setClient(new…
dev
  • 11,071
  • 22
  • 74
  • 122
42
votes
3 answers

Retrofit - Intercept responses globally

I'd like to intercept all responses received by the retrofit engine, and scan for HTTP error code, for example error 403. I'm aware I can use the failure(RetrofitError error) callback of every request and check for 403's but I'd like to wrap the…
Aviran
  • 5,160
  • 7
  • 44
  • 76
41
votes
2 answers

Rejecting re-init on previously-failed class in OkHttp

I'm trying to upload an image to my server using flask back end which will handle the file to save in a folder and OkHttp in android. but I get this error in android: I/art: Rejecting re-init on previously-failed class…
User Unknown
  • 1,079
  • 1
  • 7
  • 17
40
votes
11 answers

How to fix Expected Android API level 21+ but was 19 in Android

In my application i want get data from server, for get connect to server i used Retrofit, OkHttp. But when running application, show me force close error. In android api 21+ is not error, but below api 21 show me force close error. ApiClient class…
Jake warton
  • 2,163
  • 4
  • 11
  • 20
39
votes
1 answer

Unexpected char 0x0a in header value when using OkHttp client in Android

When sending a Base64 encoded string as header using Http, I am getting error response as Unexpected char 0x0a at 28 in header value: I99Uy+HjG5PpEhmi8vZgm0W7KDQ= Usage : String encodedHeader = Base64.encodeToString(value.getBytes(),…
priyankvex
  • 5,760
  • 5
  • 28
  • 44
39
votes
5 answers

Tracking progress of multipart file upload using OKHTTP

I am trying to implement a a progress bar to indicate the progress of a multipart file upload. I have read from a comment on this answer - https://stackoverflow.com/a/24285633/1022454 that I have to wrap the sink passed to the RequestBody and…
Jonathon Fry
  • 3,042
  • 3
  • 23
  • 30
37
votes
6 answers

Retrofit2: Modifying request body in OkHttp Interceptor

I am using Retrofit 2 (2.0.0-beta3) with OkHttp client in Android application and so far everything going great. But currently I am facing issue with OkHttp Interceptor. The server I am communicating with is taking access token in body of request,…
Lala Rukh
  • 373
  • 1
  • 3
  • 4
37
votes
8 answers

Glide - adding header to request

Is there a method to add a custom header to request when an image is downloaded? I can use volley or okhttp in Glide. I tried adding a cookie to the cookiemanager in okhttpclient, but it didn't help. Is there a method to debug request response in…
tomi
  • 381
  • 1
  • 3
  • 3
37
votes
5 answers

Android Picasso library, How to add authentication headers?

I have tried setting a custom OkHttpClient with a custom Authenticator, however as the doc says: "Responds to authentication challenges from the remote web or proxy server." I have to make 2 requests for each image, and that is not ideal. Is there a…
gonzalomelov
  • 971
  • 1
  • 13
  • 30
37
votes
9 answers

How to add parameters to api (http post) using okhttp library in Android

In my Android application, I am using okHttp library. How can I send parameters to the server(api) using the okhttp library? currently I am using the following code to access the server now need to use the okhttp library. this is the my…
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
37
votes
4 answers

How to get response body to retrofit exception?

I am trying to connect to rest service via retrofit in android application. I am getting responses. But when there is some error response from the service, conversion exception occurs and now I want to do some actions based on the response body. But…
Satya
  • 1,239
  • 2
  • 11
  • 15