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

APN causing java.net.ProtocolException: Unexpected status line

I am having an issue where some of my requests we perform are failing over cellular network. I have isolated a specific case which occurs every time. I begin with a POST and then follow it with a PATCH which results in the following error: <-- HTTP…
wesjpaul
  • 240
  • 1
  • 9
2
votes
2 answers

Uploading file from android okhttp to codeigniter

As described in the question above, I'm trying to upload a file from okhttp3 to the server. I'm always receiving 'You did not select a file to upload' in my approach from codeIgniter part. Here's my code Android: public void sendItem(File…
Fringo
  • 313
  • 5
  • 25
2
votes
0 answers

OkHttp If-None-Match header is missing

I'm creating an Android app in which I use OkHttp networking library as an HTTP client. I share a single OkHttpClientinstance with a cache size of 10 MB. The flow is: I send a request to a server, the server handles it and produces the ETag header,…
mars885
  • 73
  • 3
  • 9
2
votes
0 answers

SocketTimeOutException when using okhttp android in broadcast receiver

I need to send data to server for every 3 minutes. I am using alarm Manager to trigger for every 3 minutes and I am using asynctask in broadcast receiver but I am getting error as SocketTimeoutException in okhttp. How to solve this. I checked method…
Shadow
  • 6,864
  • 6
  • 44
  • 93
2
votes
1 answer

okhttp content-length is -1 with big files

I am downloading a file with okhttp and things work fine - now I want to show the progress and hit a road-bump. The returned content-length is -1. It comes back correctly from the server: ⋊> ~ curl -i…
ligi
  • 39,001
  • 44
  • 144
  • 244
2
votes
1 answer

java okhttp manually close connection issues

When I try to use okhttp to get content from web sites, I found that I am unavailable to manually close the established sessions (using netstat to check), of course I know this is one of feature that natively support by okhttp, but in my case, the…
Andy Chan
  • 277
  • 5
  • 16
2
votes
2 answers

Java okhttp issues in redirect count and domain cert

I have three issues when I use okhttp to get content from these web sites: http://www.wp.com has error with: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed:…
Andy Chan
  • 277
  • 5
  • 16
2
votes
3 answers

Android JSON POST with OKHTTP

I´m looking for a solution to implement a JSON-POST request with OKHTTP. I´ve got an HTTP-Client.java file which handles all the methods (POST, GET, PUT, DELETE) and in the RegisterActivity I´d like to POST the user-data (from the input fields)…
Andy
  • 129
  • 1
  • 3
  • 15
2
votes
1 answer

Adding and Removing Interceptors

I have a scenario where I add an interceptor containing an Authorization String in the header to get back a token from the API. When I get back a token, I wish to add a new interceptor which will add the received token to the header of all…
Subby
  • 5,370
  • 15
  • 70
  • 125
2
votes
1 answer

Adding common parameters to different apis in retrofit2

I have 10+ different apis right now, and I want to add some common request parameters per api, the ideal way is having interceptors per api, which is impossible in retrofit. Is there a good way to do this?
baozi
  • 352
  • 4
  • 6
2
votes
2 answers

java.lang.NoSuchMethodError: okhttp3.internal.http.HttpEngine.recover

I have an application which uses ksoap with an underlying okhttp connection. The majority of the time it works - however during a connection retry the application crashes with a NoSuchMethodError. Stack trace below Fatal Exception:…
Dan
  • 41
  • 2
  • 9
2
votes
0 answers

Retrofit2 Okhttp3 catch TimeOut

I got a client like this: public enum RestClient { INSTANCE; private static final int CONNECTION_TIMEOUT = 10; private static final int READ_TIMEOUT = 30; private static final int WRITE_TIMEOUT = 30; private final Rest restClient; private…
LinkOut
  • 273
  • 2
  • 4
  • 13
2
votes
0 answers

Better approach to cache the POST request in Retrofit(2.0) with Okhttp

In retrofit, caching the GET request is easy! After googling for sometime, got to know that we can't cache the POST request through Retrofit/OkHttp. Is there any workaround or better approach to handle this limitation? In GET request, url is the key…
Karthi R
  • 1,338
  • 10
  • 25
2
votes
1 answer

Any way to disable or get around checkNameAndValue exceptions on OkHttp?

I'm trying to make a call with OkHttp and I'm getting an exception because one of the headers has an issue. The header is: Content-Disposition: attachment; filename="O Último Caçador de Bruxas (2016) 5.1 CH Dublado 1080p.mp4" I have no control over…
casolorz
  • 8,486
  • 19
  • 93
  • 200
2
votes
1 answer

Use OkHttp to upload file but occur java.lang.IllegalStateException : closed at okio.RealBufferedSink.write(RealBufferedSink.java:44)

The issue is what the title said. It only occur sometimes and some phone. Hear is the log that I…
黎卓凡
  • 21
  • 2
1 2 3
99
100