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 play video stream from websocket url on exoplayer?

I am trying to stream a video from websocket url in android. The url looks something like this ws://abc.com:80/api/streaming/download/mp4/ . I am not really sure how to proceed because this thing is new for me. I tried searching on the internet and…
Qandeel Abbassi
  • 999
  • 7
  • 31
2
votes
1 answer

Retrofit 2 + rx - How Check Response Origin (Network, Cache, or Both) in android?

I am connecting to webService like bellow : private CompositeDisposable mCompositeDisposable; PublicApi publicApi = retrofitApi.getClient("https://xxx.xxx.xxx", context, 1); mCompositeDisposable.add(publicApi.language("getLanguages") …
user4813855
2
votes
4 answers

Unexpected host with OkHttp

I am trying to send a query through a get request, the problem is I keep getting java.lang.IllegalArgumentException: unexpected host HttpUrl url = new HttpUrl.Builder() .scheme("http") .host("10.0.2.2" + "/api/" + 7) //…
TheFairywarrior
  • 718
  • 1
  • 8
  • 16
2
votes
1 answer

Creating new instance of OkHttpClient is slow (500ms)

I have a problem creating a new instance of the OkHttpClient class. It takes between 500-600 ms. import okhttp3.OkHttpClient; OkHttpClient client = new OkHttpClient(); I have a problem only on Motorola TC55 with Android api 16. I use the OkHttp…
nasdaq
  • 25
  • 5
2
votes
1 answer

Okhttp - Certificate Pinning and Public Key Pinning

I want to improve safety of my Android application. I am using OkHttp version 3. How to: 1) use Certificate Pinning with OkHttp. 2) use Public Key Pinning with OkHttp. When I am doing this: httpClient.certificatePinner(new…
2
votes
1 answer

Android Retrofit GET @Query ArrayList

I am trying to consume a GET API that expects in its query params list an array of strings. I am using Retrofit for the task. In the API interface, I have the following defined: @GET("user/explore") Observable fetchUsers(@Query("who") String…
Fouad
  • 855
  • 1
  • 11
  • 30
2
votes
2 answers

OkHttp 3 IllegalStateException

I customize an interceptor to refresh token when it's expired. When I get response body to detect if the token's expired, it throw java.lang.IllegalStateException: closed. Here is my code with OkHttp 3 @Override public Response intercept(Chain…
Hoang Lam
  • 523
  • 8
  • 23
2
votes
0 answers

Retrofit timeout not changing despite being set to the OkHttp client

I have a singleton class for network connections. I want the timeout increased and have written the following code. BizAnalystApiv2 provideBizAnalystApiV2(final ObjectMapper mapper) { final HttpLoggingInterceptor logging = new…
suku
  • 10,507
  • 16
  • 75
  • 120
2
votes
2 answers

How to fix conflicting import?

I am building an application for android and i have too conflicting imports. import com.twitter.sdk.android.core.Callback; import okhttp3.Callback; mLoginButton.setCallback(new Callback() { @Override …
arinze
  • 439
  • 1
  • 6
  • 27
2
votes
0 answers

Retrofit NetworkIntercptor and "Accept-Encoding: gzip" header

I am using retrofit in an android app Why if add custom implementation of a NetworkInterceptor the request add the header "Accept-Encoding: gzip" and I must manually decompress the body response? If I add the same custom interceptor as…
aorlando
  • 668
  • 5
  • 21
2
votes
1 answer

How to provide 2 Retrofit interfaces width different levels of logging?

My concern is about how much OkHttp log should be printed out. On certain screens of my app, there are way too many API calls --> logcat is flooded so I'd like to limit/ turn off logging there except for one-off debugging I'm new to Dagger 2 and…
ericn
  • 12,476
  • 16
  • 84
  • 127
2
votes
0 answers

How can release unused resource.Thread( OkHttp ConnectionPool ) in OkHttp

I am invoking evictAll from connectionPool and cancelAll from dispatchers. but it is not killing the thread ( OkHttp ConnectionPool ). Here is the Thread stacktrace. [info]java.lang.Object.wait(Native Method) [info]…
sewak
  • 21
  • 3
2
votes
1 answer

Unexpected end of stream on large payload OkHTTP

I am using HttpsURLConnection which uses okhttp, and everytime I use PUT and send a payload of around 5KB it sends it without any errors; nevertheless, when I send larger payloads of over 10KB, I would get the following error: W/System.err:…
2
votes
1 answer

OkHttp POST body not received

Am using okhttp to send and receive data to and from the server not working as expected or am I doing something wrong. My Android Client Side, bear in mind I have already instantiated the client okHttp object and my containing class already…
DaviesTobi alex
  • 610
  • 1
  • 9
  • 34
2
votes
0 answers

equivalent class RetrofitError in retrofit 2.0

I'm looking for equivalent class RetrofitError in retrofit 2.0 I do sync call and need status code, headers, body, etc. For async call it is easy but how to do it with async? My code: try { Response execute = call.execute(); } catch…
Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51