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

Retrofit2: serializer doesn't get called for @Field-annotated parameters

I need to send HTTP PUT request with custom JSON object in one of request's parameter. Here is a problem: when I use it with Retrofit2, my serializer doesn't called. My object should looks like this: { "ignore":["item1", "item2"] } When I call…
BArtWell
  • 4,176
  • 10
  • 63
  • 106
2
votes
1 answer

How do I encrypt an Okio BufferedSource?

Due to HIPAA requirements, I need to encrypt data at rest. I have a BufferedSource that contains data from my api call. The Okio documentation says Sinks may be layered to transform received data, such as to compress, encrypt, throttle, or add…
Spencer
  • 311
  • 1
  • 3
  • 7
2
votes
1 answer

How do I use okhttp in the spring cloud ribbon

The getting started of the spring cloud ribbon is very easy and simple, and it is using the rest template to communicate with backend servers. But in our project we are more like to use okhttp to do the http request, does anyone can help?
2
votes
1 answer

How should I use Strings in the REST API code of my android app?

I'm building an Android app to that consumes a REST API. I'm using OkHttp and the API I'm hitting requires an API key. I thought it would be best practice to store my API key in my strings.xml file. I also thought it was a best practice to not…
intA
  • 2,513
  • 12
  • 41
  • 66
2
votes
0 answers

Dagger 2 OkHttpClient Authenticator with AccountManagerFuture

I'm writing an Android application with Dagger 2, Retrofit and I want use the AccountManager utilities. I need to provide the OkHttpClientwith a specific Authenticator that refresh the auth-token using the AccountManagerFuture calls. The code is…
Samuele Colombo
  • 685
  • 2
  • 6
  • 20
2
votes
3 answers

I tried from POSTMAN API working fine but not from my Java code?

Please I have gone through all the question from stackoverflow, but those are not applicable to my problem. Please have look in image request working fine from POSTMAN. But When I tried from android code it is not working. My Sample Android code is…
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
2
votes
1 answer

Java - Retrieving Result from OkHttp Asynchronous GET

So I have a web-app in Spring Boot, and there is a part where I make many HTTP requests to an API, and it seems to timeout if too many requests are made. I heard that switching from Synchronous to Asynchronous requests might help this issue. Using…
Alex Hermstad
  • 330
  • 5
  • 14
2
votes
1 answer

What is wrong with upload speed of some devices with Multipart OkHttp3/Retrofit 2

I have an App that can record and upload video file. I have a mechanism that logs onFailure Throwable on every file upload. I have noticed that from a thousand devices using the App, there are some that have constant timeouts and have poor success…
2
votes
1 answer

How to improve okhttp3 performance during cold startup

I've run nimbledroid profiler and I've came across many ocurrences following method chain: Is there a way to cache or bypass systemDefaultTrustManager?
pixel
  • 24,905
  • 36
  • 149
  • 251
2
votes
1 answer

ClassNotFoundException: android.net.ZeroBalanceHelper

I'm having this error on a con.getResponseCode() call. The crash occurs in a service that is performing a HEAD request. I haven't run the code in quite a while, and it worked before. Since then I've updated my test device from 5.1 to 6.0.1 // Open…
rubenwardy
  • 679
  • 5
  • 21
2
votes
2 answers

Retrofit keeps retrying when internet is not connected

I have successfully implemented Retrofit 2 in my project and everything is working as expected. But once internet is not present, the the app is sending multiple requests for the failed request. This creates a stack of failed requests that keep…
2
votes
1 answer

Can't access some member of JSON array Android

I have JSON array like this [ { "ride_id": "48", "user_id": "22", "event_id": "42", "driver_id": "0", "pick_up_location": "11111" }, { "ride_id": "48", "user_id": "21", "event_id": "42", "driver_id": "0", …
styx
  • 1,852
  • 1
  • 11
  • 22
2
votes
0 answers

SocketException with Retrofit

I am using retrofit and it works fine, but sometimes it does not get the connection and throws an exception of type SocketException, here are the configurations I use: Version: ` 'com.squareup.okhttp3: okhttp: 3.5.0' …
2
votes
1 answer

Getting Null response even the request is successful in Retrofit 2

I am getting null in Response body in Retrofit 2. whereas in body i have valid data. //using retrofit HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); …
Deepak nigam
  • 99
  • 1
  • 15
2
votes
1 answer

How do I consume Twitter Streaming API in android using okhttp3?

I want to keep consuming the stream response. This is my code so far. I think I am not able to keep the connection open for long. OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer("**********", "**********"); …
sachinsharma
  • 195
  • 1
  • 12