by keen

Setup with Gradle

Simply add this line to your app/build.gradle file:

Note: If you are upgrading from an older version of OkHttp, your imports will also need to be changed from import com.squareup.okhttp.XXXX to import okhttp3.XXXX.

Sending and Receiving Network Requests

If there are any query parameters that need to be added, the HttpUrl class provided by OkHttp can be leveraged to construct the URL:

If there are any authenticated query parameters, headers can be added to the request too:

Synchronous Network Calls

Asynchronous Network Calls

We can also make asynchronous network calls too by creating a Call object, using the enqueue() method, and passing an anonymous Callback object that implements both onFailure() and onResponse().

OkHttp normally creates a new worker thread to dispatch the network request and uses the same thread to handle the response. It is built primarily as a Java library so does not handle the Android framework limitations that only permit views to be updated on the main UI thread. If you need to update any views, you will need to use runOnUiThread() or post the result back on the main thread.

Processing Network Responses

Assuming the request is not cancelled and there are no connectivity issues, the onResponse() method will be fired. It passes a Response object that can be used to check the status code, the response body, and any headers that were returned. Calling isSuccessful() for instance if the code returned a status code of 2XX (i.e. 200, 201, etc.)

The header responses are also provided as a list:

The headers can also be access directly using response.header():

We can also get the response data by calling response.body() and then calling string() to read the entire payload. Note that response.body() can only be run once and should be done on a background thread.

Processing JSON data

We can also decode the data by converting it to a JSONObject or JSONArray, depending on the response data:

Sending Authenticated Requests

OkHttp has a mechanism to modify outbound requests using interceptors. A common use case is the OAuth protocol, which requires requests to be signed using a private key. The OkHttp signpost library works with the SignPost library to use an interceptor to sign each request. This way, the caller does not need to remember to sign each request:

Caching Network Responses

We can setup network caching by passing in a cache when building the OkHttpClient:

We can control whether to retrieve a cached response by setting the cacheControl property on the request. For instance, if we wish to only retrieve the request if data is cached, we could construct the Request object as follows:

We can also force a network response by using noCache() for the request:

We can also specify a maximum staleness age for the cached response:

To retrieve the cached response, we can simply call cacheResponse() on the Response object:


Use Facebook's Stetho plugin to monitor network calls with Chrome:

Add this line to your Gradle configuration:

When instantiating OkHttp, make sure to pass in the StethoInterceptor.

Finally, make sure to initialize Stetho in your Application: