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

Can't get Retrofit 2 to send globally-set headers with Dagger 2

I've been wanting to adopt Dagger 2 in conjugation with Retrofit 2. All seems to work nicely except for GET requests; they doesn't seem to have any headers attached with them. Below is my NetworkModule which provides all networking-related…
Hadi Satrio
  • 428
  • 2
  • 8
2
votes
6 answers

Android Retrofit 2 file upload is not working

I want to develop an app which is upload any type of file to sever. I got source code. But it is working only for images. And only upload below 2 MB size of image files. I want to upload all type of files. Please help me. My codes are shown…
fazil tm
  • 299
  • 1
  • 5
  • 23
2
votes
0 answers

Android DownloadManager Behaviour when using okhttp

Why BufferSize of RealBufferedSource(FixedLengthSource) changes from 65536 to 2048 bytes when there is an exception? 2 issues i m facing on Downloadmanager code which are as follows: (I m new to this forum. If any more inputs needed, please tell…
ramsarvan
  • 53
  • 1
  • 5
2
votes
1 answer

How to resolve error - package com.squareup.okhttp3 doesn't exist?

I am trying to convert JSON to JAVA object with the use of GSON in MAVEN, I am following a youtube video for guidance - https://www.youtube.com/watch?v=Vqgghm9pWe0 , however theres an error which occurs when in the Main Class the error is - package…
Ardi Abdul
  • 23
  • 1
  • 6
2
votes
2 answers

Okhttp3 is not see WebSocket interface in Android Studio

I get some problems with okhttp3 lib. I added the following line in my gradle file dependency: compile 'com.squareup.okhttp3:okhttp:3.4.1' After that, I try to create webSocket connection like okhttp websocket example, but in Android Studio I…
Oleg Skidan
  • 617
  • 6
  • 24
2
votes
3 answers

java.io.IOException: stream was reset: REFUSED_STREAM on several devices

I'm using Retrofit 1.9 in my Android app. Server is on Django with Nginx, HTTPS with HTTP 2. Certificate is from WoSign, "A" score on SSL Labs. I'm testing on 3 devices: Nexus 4, Android 5.1.1 (official) Nexus 9, Android 7.0 (official) Samsung…
artem
  • 16,382
  • 34
  • 113
  • 189
2
votes
1 answer

OkHTTP connection to PayPal gives an SSLHandshakeException

I'm using Java 1.7 (jdk1.7.0_80_64x) and com.squareup.okhttp okhttp 2.7.5 To connect to Paypal via this code... import…
jeff porter
  • 6,560
  • 13
  • 65
  • 123
2
votes
1 answer

Handle timeout in OkHTTP

How can i catch the timeout in OkHTTP? Is it "called" in the onFailure method? I would like to handle the situation in which i have bad internet connection. In case of timeout i have to enable some buttons to allow the user to try again. // Get a…
2
votes
0 answers

A url that can be accessed on chrome but not on OkHttp

example : http://latex.codecogs.com/gif.latex?S=%5Cpi%20r%5E2 From this url (As well as other url under http://latex.codecogs.com which contains space, or "%20"), I can get a image showing "S = πr^2" on Chrome (both PC and mobile), but only get 400…
daquexian
  • 169
  • 3
  • 17
2
votes
0 answers

Retrofit: read from cache on connection timeout

Is there a way to read from cache if a connection timeout occurs? All the cache is working, but it only works if there's no internet. I already know how to check the timeout exception: @Override public void onFailure(Call call, Throwable t) { …
Grender
  • 1,589
  • 2
  • 17
  • 44
2
votes
2 answers

How can I use Socks5 proxy in Okhttp to start http request

How can I use Socks5 proxy in Okhttp to start http request ? My code: Proxy proxy = new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved( "socks5host", 80)); OkHttpClient client = new OkHttpClient.Builder() …
zzz zzz
  • 89
  • 1
  • 5
2
votes
1 answer

Impact of setting custom HTTP Client Cache on Picasso Client

I am using Picasso to download images, and have custom OKHttp download client implementation in which we have created a custom cache of around 153MB. I'm wondering if this will override the default memory cache that Picasso has 15% of the allotted…
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
2
votes
1 answer

Picasso with OKHttp not displaying image: log error

I'm trying to download and cache an image with picasso from a webserver. I found a solution right here: https://stackoverflow.com/a/30686992/6884064 Picasso.Builder builder = new Picasso.Builder(this); builder.downloader(new…
Sara Lince
  • 119
  • 9
2
votes
0 answers

Effecting UI changes from a for loop in an asynchronous execution

for (int i = 0; i < userdataarray.length(); i++) { JSONObject jsonobject = userdataarray.getJSONObject(i); String lat = jsonobject.getString("lattitude"); String lon = jsonobject.getString("longitude"); …
2
votes
1 answer

How to download a file with progress updates using reactive extensions on Android

I am currently programming an app that needs to do lots combining of api-calls and is therefore using rxjava for the first time since it seems more convenient for handling async events and the Android lifecycles. However the app also needs…
Markus
  • 33
  • 1
  • 5