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
57
votes
4 answers

Getting Header from Response (Retrofit / OkHttp Client)

I am using Retrofit with the OkHttp Client and Jackson for Json Serialization and want to get the header of the response. I know that i can extend the OkClient and intercept it. But this comes before the deserialization process starts. What i…
dknaack
  • 60,192
  • 27
  • 155
  • 202
57
votes
5 answers

How can I pin a certificate with Square OKHTTP?

I think I need to create a new SSL Socket Factory? Also, I don't want to use the global SSL Context (https://github.com/square/okhttp/issues/184) for obvious reasons. thanks! EDIT: As of okhttp 2.1.0 you can pin certificates very easily. See the…
Michael Barany
  • 1,639
  • 1
  • 12
  • 15
57
votes
7 answers

how to use okhttp to upload a file?

I use okhttp to be my httpclient. I think it's a good api but the doc is not so detailed. how to use it to make a http post request with file uploading? public Multipart createMultiPart(File file){ Part part = (Part) new…
user2219372
  • 2,385
  • 5
  • 23
  • 26
56
votes
5 answers

OkHttp/Retrofit default timeout

I was wondering how many seconds should I set to my retrofit client. How many seconds should I use as default timeout? What is the default timeout for OkHttp/Retrofit, should we let default values?
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
54
votes
2 answers

NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/Platform

I'm using Retrofit2 library. I already tried to update latest version : Retrofit2, Gson, Rxjava, OKHttp, HttpLoggingInterceptor ... in build.gradle file build.grade in application dependencies { compile fileTree(dir: 'libs', include:…
Huy Tower
  • 7,769
  • 16
  • 61
  • 86
54
votes
3 answers

How to implement cookie handling on Android using OkHttp?

Using OkHttp by Square https://github.com/square/okhttp, how can I: Retrieve a cookie returned from the server Store the cookie for upcoming requests Use the stored cookie in subsequent requests Update the cookie returned by the subsequent…
Daniel
  • 541
  • 1
  • 5
  • 3
53
votes
4 answers

Handle exceptions thrown by a custom okhttp Interceptor in Kotlin Coroutines

I'm using a custom Interceptor along with Retrofit client in my Android app, that throws an Exception under some specific circumstances. I'm trying to make it work using Kotlin coroutines. The problem is that I'm unable to handle the before…
mrpasqal
  • 1,000
  • 1
  • 6
  • 26
53
votes
6 answers

SocketTimeoutException in Retrofit

I am trying to POST request to server for fetch data but sometime It's occure SocketTimeoutException! I used Ok3Client to resolve it but I facing the same Exception How can I resolve it? My code is below public void getNormalLogin() { if…
user5418227
52
votes
4 answers

Retrofit and OkHttp basic authentication

I am trying to add basic authentication (username and password) to a Retrofit OkHttp client. This is the code I have so far: private static Retrofit createMMSATService(String baseUrl, String user, String pass) { HttpLoggingInterceptor…
4ndro1d
  • 2,926
  • 7
  • 35
  • 65
52
votes
2 answers

Android Retrofit 2, differences between addInterceptor & addNetworkInterceptor for editing responses

I've been trying to implement an interceptor ( OkHttp 3.2 & Retrofit 2 ) for editing the JSON response before is returned as response. The server we request data returns different data dependes on success or error and that makes difficult to map the…
Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60
49
votes
3 answers

Is it possible to disable following redirects in OkHttp 2.0?

In Android, I'd like to use the new OkHttp 2.0 to request some URLs, but I'd like more control over redirects. I've already found the option to enable or disable following HTTPS → HTTP or HTTP → HTTPS redirects, but I'd like to not follow any…
Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
48
votes
5 answers

How to make OKHTTP post request without a request body?

Is there any way to make a post request with OkHTTP that does not have a request body?
Justcurious
  • 2,201
  • 1
  • 21
  • 36
45
votes
5 answers

Retrofit Uploading multiple images to a single key

I am using Retrofit to upload images to my server. Here I need to upload multiple images for a single key. I have tried with Postman web client it is working well. Here is a screenshot. Here are the key value pairs for the request. SurveyImage :…
Kartheek
  • 7,104
  • 3
  • 30
  • 44
45
votes
1 answer

Intercept and retry call by means of OkHttp Interceptors

I need to retry request inside of OkHttp Interceptor. For example there is incoming request which needs Authorization token. If Authorization token is expired, server returns response with 403 code. In this case I am retrieving a new token and…
Araz Abishov
  • 1,661
  • 3
  • 15
  • 26
44
votes
5 answers

Retrofit API call receives "HTTP FAILED: java.io.IOException: Canceled"

Can't figure out why is this happening. Neither one of rx callbacks (onCompleted(), onError(), onNext()) not gets triggered by my call. The only thing i receive is this okhttp output: D/OkHttp: --> GET…
oleg.v
  • 1,065
  • 3
  • 11
  • 20