Discover OkHttp, a powerful HTTP & HTTP/2 client for Android applications

Created by Square, OkHttp is an open source project designed to be an efficient HTTP and HTTP/2 client. It lets you to make fast requests and save bandwith. It offers some great features out of the box like to share a socket for all HTTP/2 requests to the same host, a connection pooling mechanism when HTTP/2 is not available, transparent GZIP feature to reduce download sizes and a caching mechanism for responses. Besides, OkHttp has a great mechanism to manage common connection problems.

Image for post
Image for post

OkHttp offers a request / response API that makes developers life easier. It supports synchronous and asynchronous calls. Even better, OkHttp is also available for Java projects with Java 7 as requirement.


To use OkHttp in your Android application, you need to add the dependency in your build.gradle file like that :

compile 'com.squareup.okhttp3:okhttp:3.2.0'

Synchronous Get

First use case for OkHttp is to make a simple synchronous get from a file on the Web. Here, we choose to get a file on the SSaurel’s server : /tmp/toto.txt :

OkHttpClient client = new OkHttpClient();

Asynchronous Get

In Android applications, it’s recommended now to make network requests in separate threads. So, it would be better to download our toto.txt file on a worker thread and get called back when the response is readable. You can use the following code :

OkHttpClient client = new OkHttpClient();

Make a Post request

Now, you can use OkHttp to make a post request with a JSON content for example :

public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");


OkHttp is an elegant and powerful library to make HTTP and HTTP/2 calls on Android and more globally in Android applications. Lightweight and open source, OkHttp has a lot of more options that you could discover when you will use it.

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store