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

Add headers to request using Retrofit 2

I'm trying to send requests with authentication headers, but it seems that the server cannot identify the client. I used this tutorial, and implemented an interceptor as follows: public class AuthenticationInterceptor implements Interceptor…
Omer Amiel
  • 21
  • 1
  • 5
2
votes
1 answer

Is there a way to replace a library inside an APK without having the source code

I am doing research about some http libraries and I had to modify some of the libraries. Part of the research is to test it on closed source apps so I am wondering if there is a way to replace a library inside an APK app without having the source…
DigitalPerson
  • 191
  • 3
  • 12
2
votes
2 answers

How to use okhttp, to operate the files on the Webdav?

Because Sardine or Jackrabbit implementations rely on httpclient, but httpclient has been abandoned by Android. I wrote the Java code is always unable to compile correctly, some because of the multi dex error, some because not found INSTANCE or…
Donething
  • 31
  • 4
2
votes
1 answer

How to display and interact with OKHTTP html response in android Studio using webview or Web browser

I am building an android app. I have build a request using OKHTTP and I get the response as a string composed of html css and js content. This response is actualy a form that the user must use to allow the app to communicate with a given website.…
Kamba-Bilola Ted
  • 197
  • 4
  • 12
2
votes
0 answers

Retrofit with JWT Authentication - How to inform the interceptor about the current user?

I am using Retrofit 2.3.0 to talk to an API that uses JWT for authentication from a Spring Boot application. To make it work, I created an Interceptor implementation: private static class JwtAuthenticationInterceptor implements Interceptor { …
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
2
votes
1 answer

Move multiple files uploaded via POST Http request (okhttp 3.3.1) to another directory

I need to upload multiple files per request from my Android application to an apache webserver. I am on a Windows 10 computer and used XAMPP to set up the webserver. When I upload a single file, everything works as intended. But when posting…
2
votes
1 answer

How to remove url from Cache in okhttp?

I use Retrofit 2 library with okhttp3 to make API calls in my android app. I need to invalidate cache for some specific urls after some operations(invalidate get posts after creating a new post). On the internet everyone says I need to get Cache…
Elnur Hajiyev
  • 464
  • 1
  • 4
  • 14
2
votes
1 answer

OkHttp CookieJar saveFromResponse method

I created a simple project which using CookieJar. Now I am trying to understand when saveFromResponse method works. But I see in my logs that loadForRequest works fine, but I doesn't see saveFromResponse logs. Why? At what time of process this…
Delphian
  • 1,650
  • 3
  • 15
  • 31
2
votes
0 answers

The most secure network library for Android

Recently I have used the retrofit 2.3.0 networking library in my project. I have also implemented SSL pinning for securing the app, using the CertificatePinner class in the Okhttp and thus Retrotif. However, after another team has run a…
Farhad
  • 12,178
  • 5
  • 32
  • 60
2
votes
3 answers

How can I put the data to RecyclerView from okhttp and Asynctask

I've get some Json data from server by okhttp ,and I want to show them in a recycleview inside of a fragment using a custom adapter and receiving the following error. when I run this code,the logcat tell me the followeing…
2
votes
0 answers

Android ImageView PostInvalidate not working all the time

I have extended an ImageView in Android where I download an image, and when the image arrives I invalidate the view to get a redraw. If I use caching it's not working all the time, so I suspect that android is ignoring some invalidate commands if…
fred_
  • 1,486
  • 1
  • 19
  • 31
2
votes
1 answer

Load SVG from file in Android

I want to display a .svg file in an imageview or a webview. I don't know which one is better for this purpose, would like some opinions on this subject. Keep in mind that I don't have the file on compile time so I can't use the VectorDrawable API…
Exprove
  • 1,301
  • 2
  • 18
  • 32
2
votes
1 answer

Okhttp3 get cookie

I am doing this request : OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, "username=George&password=toto"); …
Loris.Foe
  • 21
  • 1
  • 3
2
votes
1 answer

Why doesn't OkHttp encode the percent sign?

From HttpUrl.java: static final String FORM_ENCODE_SET = " \"':;<=>@[]^`{}|/\\?#&!$(),~"; From FormBody.java public Builder addEncoded(String name, String value) { names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, false, true, true,…
2
votes
1 answer

Android - Certificate Pinning with Retrofit 2.3 and OkHTTP

Hello dear developers, I have the following Problem: I´m using Certificate Pinning successfully since a few months now, with OkHTTP 3.6 and Retrofit 1.9.0. Recently I updated the used Retrofit version to 2.3.0 and with this started using OkHttp 3.8.…
Koerfer_P
  • 341
  • 2
  • 13