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
36
votes
3 answers

OkHttp Library - NetworkOnMainThreadException on simple post

I want to use OkHttp library for networking in Android. I started with the simple post example as written in their website: public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new…
Aviv Ben Shabat
  • 1,073
  • 2
  • 13
  • 33
36
votes
5 answers

Uploading a large file in multipart using OkHttp

What are my options for uploading a single large file (more specifically, to s3) in multipart in Android using OKhttp?
Tomer Weller
  • 2,812
  • 3
  • 26
  • 26
35
votes
2 answers

Retrofit and OkHttp gzip decode

The REST service I want to consume answers as a gzipped encoded JSON. It provides the Content-Encoding: gzip, but my OkHttp does not encode it to readable text, so the JSON converter throws an exception. ---> HTTP GET…
Simon Tenbeitel
  • 835
  • 1
  • 8
  • 20
35
votes
7 answers

OkHttp javax.net.ssl.SSLPeerUnverifiedException: Hostname domain.com not verified

I've been trying for days to get this working. I'm trying to connect to my server over https with a self signed certificate. I don't think there is any pages or examples that I haven't read by now. What I have done: Created bks keystore by…
just_user
  • 11,769
  • 19
  • 90
  • 135
34
votes
1 answer

Retrofit "IllegalStateException: Already executed"

I have a Retrofit network call that id like to run every 5 seconds. My current code: Handler h = new Handler(); int delay = 5000; //milliseconds h.postDelayed(new Runnable() { public void run() { call.enqueue(new Callback()…
Orbit
  • 2,985
  • 9
  • 49
  • 106
34
votes
2 answers

OkHttpClient throws exception after upgrading to OkHttp3

I'm using following lines of code to add a default header to all of my requests sent using Retrofit2: private static OkHttpClient defaultHttpClient = new OkHttpClient(); static { defaultHttpClient.networkInterceptors().add(new Interceptor() { …
Ashkan Sarlak
  • 7,124
  • 6
  • 39
  • 51
34
votes
2 answers

How to tell OkHttpClient to ignore cache and force refresh from server?

In my android application, I am using Retrofit with OkHttpClient with caching enabled to access some APIs. Some of our APIs sometimes return empty data. We provide a "Refresh" button in the app for the client to reload data from a specific API. How…
inder
  • 1,774
  • 1
  • 15
  • 15
34
votes
3 answers

Get Http Status Code with OkHttp

I'm using OkHttp to get the content of some websites. However, I'm not able to get the Http-Status Code from the response. My Java-Code: OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() …
maja
  • 17,250
  • 17
  • 82
  • 125
33
votes
2 answers

java.net.SocketTimeoutException in okhttp

Here it takes so much time to get data from json. When deleted and again installed it will get json within 1 min and when i again click button for json it takes so much time and still data is not getting in listview Here is my exception…
Abhi
  • 385
  • 1
  • 4
  • 13
33
votes
3 answers

OkHttp proxy settings

I have to setup a proxy to send a JSON using POST, using proxyHost and proxyPort. public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); Proxy proxyTest = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy",…
Matheus Bica
  • 1,055
  • 3
  • 13
  • 23
33
votes
7 answers

Retrofit 2.0-beta-2 is adding literal quotes to MultiPart values

Went to upgrade to Retrofit 2.0 and running into this weird problem. I have a method to log a user in public interface ApiInterface { @Multipart @POST("user/login/") Call userLogin(@Part("username") String username,…
Brian
  • 4,328
  • 13
  • 58
  • 103
32
votes
5 answers

Retrofit: Redirect to LoginActivity if response code is 401

How to start LoginActivity from the interceptor(non-activity class)? I have tried the code (Interceptor) below but not working for me. Interceptor OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() { …
Rick
  • 541
  • 1
  • 8
  • 22
31
votes
1 answer

CertPathValidatorException: Trust anchor for certification path not found

I added HTTPPinning to OKHTTPClient the sample code is: OkHttpClient client = new OkHttpClient(); client.setSslSocketFactory(getPinnedCertSslSocketFactory(context)); private SSLSocketFactory getPinnedCertSslSocketFactory(Context…
Venkat
  • 3,447
  • 6
  • 42
  • 61
31
votes
2 answers

Android Retrofit2 Refresh Oauth 2 Token

I am using Retrofit and OkHttp libraries. I have an Authenticator that authenticates the user when we get a 401 response. My build.gradle is like this: compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile…
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
31
votes
2 answers

Using OKHttp, what is the difference between synchronous request in AsyncTask and OKhttp Asynchronous request?

OKHttp supports both synchronous and asynchronous api. If I want to issue an async request, I can: Use a AsyncTask, and issue OKhttp synchronous api. Issue a OKhttp asynchronous api. What is the difference between these 2 options? And which one…
NeoWang
  • 17,361
  • 24
  • 78
  • 126