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

okhttp retrofit2 synchronize request crash

My app uses rxjav, retrofit2, okhttp. Today it crashed: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String)' on a null object reference at…
杜泽超
  • 21
  • 2
2
votes
1 answer

Set timeout for specific call

I'm using Retrofit and OkHttp for my Android app. I followed this tutorial to create a class to handle the API client: public class ApiClient { public static final String API_BASE_URL = "https://www.website.com/api/"; private static…
julyfry
  • 21
  • 2
2
votes
1 answer

Retrofit2 OkHttp3 Response Body Null Error

I am using Retrofit 2 to call a microservice which returns a 200 and an empty response body on a PUT method. Retrofit 2 however seems to not be able to handle this and in the "enqueue" goes to the onFailure branch @Override public void…
razvan
  • 559
  • 7
  • 23
2
votes
1 answer

Retrofit/OkHttp crashes on response

I've recently started having a problem with Retrofit/OkHttp. Whenever the app performs a request it crashes with the error: 05-18 12:13:51.908 E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: <
Tudor S.
  • 809
  • 2
  • 12
  • 29
2
votes
1 answer

Relation between okhttp and HttpURLConnection

Many people talk to me that HttpURLConnection is based on okhttp from android 4.4 on. But when I dive into codes of okhttp, the OkHttpURLConnection is a subclass extended from HttpURLConnection. So, okhttp is based on HttpURLConnection or…
QuinnChen
  • 670
  • 1
  • 8
  • 29
2
votes
1 answer

Posting JSON to NodeJS server shows success when it is actually a failure

Here is my android code to post to my nodejs server registrationjson.setUsername(username.getText().toString()); registrationjson.setPassword(password.getText().toString()); Gson gson = new Gson(); String…
Behrouz Riahi
  • 1,751
  • 2
  • 21
  • 42
2
votes
1 answer

pass array or jsonobject to a php url with okhttp in android?

I want to pass two values(can be array or jsonobject in curly braces only) to a php webservice by okhttp in android and it is like: userid="1" & productid={"1","2","3"}. I know how to pass userid but I don't know how to pass productid values to…
Chirag Gohil
  • 352
  • 1
  • 2
  • 14
2
votes
1 answer

Whats wrong with this Log.d

What's wrong with Log.d() if I comment it, then no error shows, but with Log.d() values display but also get FATAL EXCEPTION: AsyncTask #1 and app crash. package com.sha.okhttp; import android.os.AsyncTask; import android.os.Bundle; import…
Shareque
  • 361
  • 4
  • 23
2
votes
1 answer

Retrofit2 dont send request (no logging with OkHttp)

I'm trying to use Retrofit2 with RxJava2, here is Api: public class App extends Application { final String SERVER_ENDPOINT = "https://api.vk.com"; private Retrofit mRetrofit; private static ImageService mImageService; …
Koroqe
  • 67
  • 7
2
votes
1 answer

Read multipart response

there are documentations on how to send a multipart message but no information I can find relates to how to receive a multipart message. I have a restful service that returns multiple files in a multipart message. I wonder what is the best way to…
JRun9
  • 31
  • 5
2
votes
1 answer

Uploading remote URI's using retrofit/okhttp stack on Android

Dear stack overflow, The concept of an URI on Android is wonderful, and it abstracts out how we point to different things on the device and internet, but it does need careful thought to work with them. I have couple of questions for you. 1)…
2
votes
0 answers

okhttp authenticator cancel request

I'm trying to manage my token expiration with okHttp. So I've read plenty of responses on Internet and they recommand to use okHttp with an Authenticator. So I've created an authenticator : public class TokenAuthenticator implements Authenticator…
Foushi
  • 365
  • 1
  • 3
  • 12
2
votes
2 answers

No HTTP request is send from retrofit /Android/

I'm trying to send data to an API from my Android project using Retrofit. Everything seems to work without errors but no http request leaves the application. I can confirm this with Wireshark screening and API console log. Here is an example pseudo…
nikitz
  • 1,051
  • 2
  • 12
  • 35
2
votes
1 answer

OkHttpClient not supporting Http 1.1 for Https network calls

I am using Retrofit 2 with OkHttpClient. I want to make connection having the keep-alive property, so as to make persistent connection with the server which is Https enabled. Now my problem arises when I am making a network call to Https end point…
saurabhlahoti
  • 466
  • 8
  • 21
2
votes
1 answer

android app stopped working when button with okhttp3 request clicked

I'm making an application with buttons for sound recording and a button to upload the recording file to server. The upload button supposed to call a function containing okhttp3 multipart request, but when i click it the application stop working. I'm…