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
22
votes
4 answers

Using RxJava and Okhttp

I want to request to a url using okhttp in another thread (like IO thread) and get Response in the Android main thread, But I don't know how to create an Observable.
user5357773
22
votes
5 answers

Android OkHttp, refresh expired token

Scenario: I am using OkHttp / Retrofit to access a web service: multiple HTTP requests are sent out at the same time. At some point the auth token expires, and multiple requests will get a 401 response. Issue: In my first implementation I use an…
ticofab
  • 7,551
  • 13
  • 49
  • 90
22
votes
5 answers

Android Enable TLSv1.2 in OKHttp

i am using OKHttp for my project. i want to enable TLSv1.2 for my service call. can any body tell me how to enable it.
Santhi Bharath
  • 2,818
  • 3
  • 28
  • 42
22
votes
4 answers

Android Volley MalformedURLException Bad URL

After making a second network request using Volley, I always get this error. It doesn't seem to matter what the url I put in is. Volley always claims it is malformed. 08-04 20:16:26.885 14453-14470/com.thredup.android E/Volley﹕ [994]…
Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
21
votes
2 answers

How to enable TLSv1.3 for OkHttp 3.12.x on Android 8/9?

I'm using OkHttp 3.12.2 on Android 9 (Pixel 2 device) and try to connect to an nginx 1.14.0 running with OpenSSL 1.1.1. The nginx is capable of TLSv1.3, I verified this with Firefox 66.0.2 on Ubuntu 18.04, Chrome 73.0 on Android 9 and ChromeOS…
Andreas
  • 529
  • 1
  • 6
  • 9
21
votes
3 answers

How to retrieve cookie from response retrofit, okhttp?

I am trying to retrieve Cookies values from API response for maintaining the backend session by setting cookie value to new API call in case the APP is closed : The response to API call from PostMan RestClient: RetrofitClient.java public static…
Anurag Dhunna
  • 544
  • 1
  • 5
  • 17
21
votes
2 answers

Okhttp or HTTPClient : Which offers better functionality and more efficiency?

To make a http request, there're some APIs alternative in JAVA, such as Apache HttpClient and Okhttp. Apache HttpClient is mature and widely used, and Okhttp seems to be more and more popular(I'm not sure). What I wondered is, which is better, or…
Coderec
  • 223
  • 1
  • 2
  • 7
21
votes
3 answers

MockWebServer response delay - testing timeouts

I'm trying to test HTTP timeout scenarios using a MockWebServer which answers my test requests sent with Retrofit/OkHttp. (This question was asked before some years ago, but at the time concerned a bug in the MockWebServer. Also, the API has since…
fgysin
  • 11,329
  • 13
  • 61
  • 94
21
votes
5 answers

Got this error with retrofit2 & OkHttp3. Unable to resolve host "": No address associated with hostname

I am using the retrofit 2 and OkHttp3 to request data from server. I just added a offline cache code but It's not working as expected. I got the error "Unable to resolve host "<>": No address associated with hostname." This occurs when It's try to…
android_griezmann
  • 3,757
  • 4
  • 16
  • 43
21
votes
4 answers

How to get raw response and request using Retrofit 2.0

I am trying to get the raw response using Retrofit2.0.2. So far I tried to print the response using following line of code but it prints the address and not the exact response body. Log.i("RAW MESSAGE",response.body().toString()); compile…
praveen kumar
  • 211
  • 1
  • 2
  • 3
21
votes
4 answers

Add cookie to client request OkHttp

So i started using Okhttp 3 and most of the examples on the web talk about older versions I need to add a cookie to the OkHttp client requests, how is it done with OkHttp 3? In my case i simply want to statically add it to client calls without…
Michael A
  • 5,770
  • 16
  • 75
  • 127
21
votes
2 answers

Replace common path parameters in retrofit 2 with okhttp

I have some service urls with the same baseUrl. For some urls there will be some common used parameters, for example an apiVersion or locale. But they don't have to be in every url, so I can't add them to the…
Siebe
  • 942
  • 2
  • 10
  • 27
21
votes
3 answers

UnknownHostException: name or service not known

I'm attempting to return some data from an API using OkHttpClient in com.squareup.okhttp. I've run into a few errors that i have eventually been able to overcome but i can't get past this host exception error and nothing on here seems to be specific…
Jordan Macey-Smith
  • 321
  • 1
  • 2
  • 6
21
votes
3 answers

How to suppress Charset being automatically added to Content-Type in okhttp

Consider the following code: OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("text/plain; charset=utf-8"); // [A] RequestBody body = RequestBody.create(mediaType, media); String[] aclHeader =…
Oliver Hausler
  • 4,900
  • 4
  • 35
  • 70
21
votes
3 answers

How to use Retrofit and SimpleXML together in downloading and parsing an XML file from a site?

I just started working with Retrofit. I am working on a project that uses SimpleXML. Can somebody provide me an example in which one fetches an XML from a site e.g. http://www.w3schools.com/xml/simple.xml" and reads it out?
greenspand
  • 749
  • 2
  • 8
  • 15