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

IncompatibleClassChangeError from okhttp3.internal.Util.closeQuietly()

Seeing the following stack on old versions of Android (4.3 and earlier): Caused by: java.lang.IncompatibleClassChangeError: interface not implemented at okhttp3.internal.Util.closeQuietly(Util.java:100) at…
Mark McClelland
  • 4,980
  • 4
  • 35
  • 48
2
votes
1 answer

Why the okhttp3.Interceptor don't work?

I add two Inteceptors and a NetworkInceptor in OkHttpClient, but only cachingInterceptor works. I don't know how to add another Interceptor to modify my request headers. And there is a picture showing that my request headers didn't be modified below…
user7434432
  • 31
  • 1
  • 4
2
votes
1 answer

when does okhttp client cache the server response?

I am using okhttp client 3.5.0 in java to cache the response and server response has below cache-control header: Cache-Control: private, max-age=0, must-revalidate First I am trying to make actual network call to get the server response, then in…
Sagar Rathod
  • 542
  • 8
  • 13
2
votes
2 answers

Call retrofit with JSONObject.

I have to make a get request from a server which gives me a lot of information. From what I found searching it seems like I can do this only with model classes. Thing is that to cover all the response the server sends to me I have to make 53 model…
Boanta Ionut
  • 402
  • 8
  • 24
2
votes
2 answers

Android webview not loading css and javascript when trying to access webpage using custom headers

I am using authorization headers to access the web page, but when using WebViewClient with authorization headers the webview not rendering the css and also the js not loading. public class TableViewTest extends AppCompatActivity { WebView…
Sudheesh R
  • 1,767
  • 3
  • 23
  • 43
2
votes
1 answer

could not GET http.../artifactory/oss-snapshot-local/..bolts-android-24.2.0.pm

the issue is that when i try to build the gradle, it throws: Error:Could not GET 'http://oss.jfrog.org/artifactory/oss-snapshot-local/com/parse/bolts/bolts-android/24.2.0/bolts-android-24.2.0.pom'. Received status code 409 from server: Conflict i…
2
votes
1 answer

How to use okhttp to put a validate code to imageView?

Request request = new Request.Builder() .url(url) .addHeader("Cookie", "JSESSIONID="+sessionId) Response response = client.newCall(request).execute(); if (response.isSuccessful()) { InputStream inputStream =…
刘恩硕
  • 21
  • 4
2
votes
1 answer

OkHttp3 Multiple request tag

I am assigning the tag to OkHttp Request like, Request request = new Request.Builder() .url(url) .tag(requestTag) .build(); and I can cancel that particular request by that using public static void cancel(Object tag) { for (Call…
Jaymin Panchal
  • 2,797
  • 2
  • 27
  • 31
2
votes
1 answer

okhttp post request sending null values to server

I'm developing simple android application that making simple post request to server. But it sending null values for both keys which are required as request body from server. I've check it from my app end but it's not sending null values. here's my…
2
votes
0 answers

Get full size of response using Retrofit or Okhttp

My first time using Retrofit2 and okhttp3 to send http request. I research many sites and APIs but there a no result about "how to get fullsize of request and reponse". I'm using HttpLoggingInterceptor to log the response, but it just shows the size…
NamNH
  • 1,752
  • 1
  • 15
  • 37
2
votes
1 answer

Android connection with ESP8266 on One Plus ( Android 6.0.1)

Retrofit on Android 6.0 has a problem making Http calls after connecting to Access Point Steps to reproduce: Connect to Esp8266 Access Point Make an http call to http://192.168.4.1 (Default gateway of esp8266 accesspoint) The IP address of WIFI is…
thehellmaker
  • 162
  • 3
  • 13
2
votes
0 answers

Android HttpsUrlConnection performing much better than OkHttp/Retrofit

I previously used an AsyncTask with javax.net.ssl.HttpsURLConnection to call my REST API but switched to Retrofit as it makes for simpler code and I read everywhere that it is faster. However, it consistently performs worse. With HttpsURLConnection…
Erik Lumme
  • 5,202
  • 13
  • 27
2
votes
2 answers

App crashing when I try and return http response

I have made a java class to handle HTTP post requests and it sends back the result in string form. For some reason when I send the request I can print the response to the log but when return the string of the response to update the UI in the method…
2
votes
1 answer

okhttp unwanted escaping of form data

I'm trying to use okhttp to build a post request to send JSON to an existing service. but when ever i try to send the form okhttp escapes all the special characters in the JSON - but the server is coded to only accept it not-escaped. String msg =…
nwodb.com
  • 55
  • 7
2
votes
2 answers

OkHttp: A connection to http://example.com/ was leaked. Did you forget to close a response body?

This error message of OkHttp v3.4.1 has already been discussed a few times, and each time I read about it, people were not closing the response body: WARNING: A connection to http://www.example.com/ was leaked. Did you forget to close a response…
Michel Jung
  • 2,966
  • 6
  • 31
  • 51