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
0 answers

Cannot get cookies set in library from CookieManager

I created a library that handles authentication, which I added as a dependency to my application. The library uses OkHttp to handle authentication, so I use a CookieJar to sync the cookies from the OkHttpClient instance with the Android…
Bryan
  • 14,756
  • 10
  • 70
  • 125
2
votes
1 answer

okhttp3 causing java.lang.NoClassDefFoundError in java maven project

I know that there's a lot of question related to this issue but none made sense for me. I built a Java Desktop application that adds products for sale by communicating with an API. I am using okhttp 3.9.0 to accomplish this task. The problem is that…
Diego Alves
  • 2,462
  • 3
  • 32
  • 65
2
votes
2 answers

Reasons to upgrade Retrofit and OKHttp in an existing Android project

I am new in an Android project that makes heavy use of a web API. The project uses: retrofit-1.9.0 and okhttp-2.0.0 for web API calls also okhttp3-3.9.1 for some other web stuff I would like to upgrade the libs to: retrofit-2.3.0 and…
RumburaK
  • 1,985
  • 1
  • 21
  • 30
2
votes
0 answers

Why doesn't Meteor 1.6.1 use the latest version (1.5.2) of cordova-plugin-meteor-webapp?

First, the larger context: We're working our way through an issue with Nginx on the server side, and the Okhttp library in our Meteor Cordova Android app. Issue is discussed here: https://github.com/square/okhttp/issues/2543 Problem summary: Hot…
Jason Schock
  • 208
  • 1
  • 11
2
votes
3 answers

Okhttp3 com.square does not exist

I am using Netbeans and I've created java project with Maven. I added this dependency. com.squareup.okhttp okhttp 2.7.2 It worked, I could import…
Diego Alves
  • 2,462
  • 3
  • 32
  • 65
2
votes
1 answer

Retrofit offline request and response

I've already read many questions and answers about my issue, but I slill can't understand how to solve it. I need to fetch response from server and store it in cache. After that when device is offline I want to use cached response. When device is…
Anton Prokopov
  • 639
  • 6
  • 27
2
votes
0 answers

Enabling TLS 1.2 with latest version of OkHttpClient

We have an Android application that uses OkHttpClient to call our REST services. We need to enable TLS 1.2 for those calls. Has anybody done this with the latest version of the OKHttpClient? I cannot find any clear sample code that just does…
Waqar Sadiq
  • 135
  • 1
  • 9
2
votes
1 answer

Redirect OkHttp3 requests on a specific network

When I have timeouts on the WiFi, I try to redirect the traffic on the mobile connection. To perform that I use a NetworkRequest and ask for TRANSPORT_CELLULAR, it gives me a Network object and I try to use it with OkHttp3. My initial (not binded…
Mathieu H.
  • 800
  • 8
  • 21
2
votes
1 answer

HTTPS Request with content-length > 0 but body is empty

I am facing a weird issue with my android application. Sometimes, the server gets a POST HTTP request with just the headers and no body. I am using Retrofit 2.2 on android with OkHttpClient 3.6. In server side logs I can see that the content-length…
Amit Barjatya
  • 249
  • 3
  • 14
2
votes
3 answers

Retrofit/OkHTTP/RxJava intermittent InterruptedIOException

I am using the following (out of date) libraries: Retrofit: 1.9.0 OkHTTP: 2.3.0 RxAndroid: 0.24.0 I noticed that every once in a while I get the following stack trace for my POST request: D/Retrofit: ---> HTTP POST https:xxxxx D/Retrofit:…
Smalls
  • 352
  • 3
  • 13
2
votes
1 answer

Does OkHttp's enqueue method implement AsyncTaskLoader?

I am pretty new to android programming. I've written a code to perform API query using the OkHttp Library. My question is, once the OkHttp's enqueue process has started, and the user immediately rotates the phone, thereby destroying and restarting…
Chao Li
  • 175
  • 10
2
votes
0 answers

OkHttp - 404 Not Found

I am using OkHttpClient to get API key but getting Response {protocol=h2, code=404, message=, url=https://unknown.com/api.php} below code, I am using. OkHttpClient client1 = new OkHttpClient(); HttpUrl.Builder urlBuilder =…
Ashish Jain
  • 328
  • 2
  • 15
2
votes
1 answer

Optimal way to handle network disconnection

I am new to Android and I am trying to figure out the best way to handle the situation when a user does not have a network connection. I am using Retrofit2 with OkHttp and I have tried multiple solutions. First approach: OkHttp interceptor: new…
Alphonse
  • 661
  • 1
  • 12
  • 29
2
votes
1 answer

Cannot connect to node websocket from android okhttp3

I've created a node server on top of expressJs and used socket.io to create a websocket server. Code i've used to create the server is io.sockets.on('connection', function (socket) { connections.push(socket); console.log('Connected: ' +…
ARiF
  • 1,079
  • 10
  • 23
2
votes
1 answer

How to make a POST request by passing a object in Okhttp?

This is my Json body : { "username": { "country": "IN", "number": "9620494812" }, "password": "119209" } I'm trying to make a POST request with Okhttp as follows: Username username = new Username("IN" ,…
Saathwik
  • 199
  • 1
  • 2
  • 11