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

OkHttp how to listen for timeout?

I've got this: final OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(someUrl) .build(); Response response = client.newCall(request).execute(); I have looked at…
b85411
  • 9,420
  • 15
  • 65
  • 119
2
votes
3 answers

java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient

I've read this question Android http connecting with OkHttp Dont work and NoClassDefFoundError for OkHttpClient but it hasn't solved my problem . Still am facing java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient this error , i have…
user6827252
2
votes
1 answer

Disable http calls over proxy , or rooted devices

final OkHttpClient okHttpClient = new OkHttpClient.Builder() .readTimeout(60, TimeUnit.SECONDS) .addInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain…
krikor Herlopian
  • 731
  • 1
  • 10
  • 23
2
votes
2 answers

android retrofit2 could not add header (415 error code)

I'm trying to access ticket data via skyscanner api and pass it to my view, but I cannot accomplish that, because I get 415 error code I'm using retrofit2 and adding header programmatically. My interface looks like this: public interface…
Fedor Tsyganov
  • 992
  • 13
  • 30
2
votes
2 answers

Android Recyclerview, okhttp, receiving an java.lang.ClassCastException error

I am trying to load data into my recyclerview via url okhttp calls, and I am receiving the following error: "java.lang.ClassCastException: SearchActivity cannot be cast to SearchForBarbershop$BarbershopRequesterResponse" Here are the…
teej2542
  • 577
  • 3
  • 10
  • 27
2
votes
0 answers

SocketTimeoutException - "timeout" "read timed out"

I am writing tests which run OkHttp/Retrofit requests against a MockWebServer. Among other things I'm testing timeouts. Here I noticed, that some of my timeout tests do not produce the same kind of exception all the time: While the exception thrown…
fgysin
  • 11,329
  • 13
  • 61
  • 94
2
votes
1 answer

How to send image and text at the same time using retrofit

I want send this Postdata and imagefile at the same time using retrofit. PostData and Point public class PostData implements Serializable { @Expose private String text; @Expose private Point point; } public class Point implements…
H.fate
  • 644
  • 2
  • 7
  • 18
2
votes
2 answers

Android - NetworkOnMainThreadException with okhttp websocket

I have a threading problem with Android okhttp Websockets. I am doing a chat application, where I need to write and read from our server using websocket. I read in other posts that with Okhttp, I should use…
techtinkerer
  • 1,280
  • 2
  • 15
  • 26
2
votes
2 answers

OkHttp: Can't send data to server via POST method

This is my first ever question on Stackoverflow. I am having issues with OkHttp while sending a simple text data to server. Basically, what I am trying is to send a piece of text to a PHP script tokenSave.php, which will then store the received data…
Manthan
  • 93
  • 1
  • 8
2
votes
2 answers

How to get HTTP Response Error Code Doing OkHttp Sync Requests

I'm using OkHttp with Retrofit to do synchronized requests. The problem is that OkHttp throws an exception. I can catch the exception in the interceptor instead, but the response is null. I'd like to display messages to the user based on HTTP…
Gehrig
  • 73
  • 3
  • 8
2
votes
1 answer

Retrofti 2: Post multiple files with part names along with @FormUrlEncoded formData

I have a rather complex POST I am trying to make with Retrofit2. @Multipart @FormUrlEncoded @POST("post") Call createPost( @Field("name") String name, @Part("media_1") MultipartBody.Part mediaOne, …
Boxwood
  • 71
  • 1
  • 2
  • 8
2
votes
1 answer

Network connection can stop in power saving mode, with java.net.SocketTimeoutException

When investigating one of the reports I got from users, I noticed that when the Power-Saving mode is enabled (Nexus 4, Android 5.1.1), the background service that downloads data gets a connection exception after some time. This happens when the…
mhenryk
  • 551
  • 5
  • 13
2
votes
0 answers

TLSv1.2 protocol on android 4.* <

I'm using okhttp v 2.4.0 with retrofit v1.9.0 to make https request where the server uses Protocol TLSv1.2 only, and I'm getting the error: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted:…
tiagoMissiato
  • 305
  • 3
  • 16
2
votes
2 answers

java.lang.NoSuchMethodError - okhttp3 (Android)

My app keeps crashing. Can't figure out where the issue is. I'm not even using okttp3 library. Currently, I'm only using retrofit 1.9 and otto 1.3.8 java.lang.NoSuchMethodError: No virtual method callEnqueue(Lokhttp3/Call;Lokhttp3/Callback;Z)V in…
LEE
  • 3,335
  • 8
  • 40
  • 70
2
votes
2 answers

Accessing OKHttp Response Body

So I need to figure out how to access the value I get from my first response in my second. I would think that I could just store it to a a variable and access it in another request. However, that does not seem to be the case. Here is the bit that is…