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.
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.
Installation
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 : http://www.ssaurel.com /tmp/toto.txt :
OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://www.ssaurel.com/tmp/toto.txt") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { // manage error Log.e("Unexpected code " + response); return; } Headers responseHeaders = response.headers(); // show response headers for (int i = 0; i < responseHeaders.size(); i++) { Log.i(responseHeaders.name(i) + ": " + responseHeaders.value(i)); } // show body content Log.i(response.body().string());
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(); Request request = new Request.Builder() .url("http://www.ssaurel.com/tmp/toto.txt") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // manage failure ! } @Override public void onResponse(Call call, Response response) throws IOException { if (!response.isSuccessful()) { // manage error Log.e("Unexpected code " + response); return; } Headers responseHeaders = response.headers(); // show response headers for (int i = 0; i < responseHeaders.size(); i++) { Log.i(responseHeaders.name(i) + ": " + responseHeaders.value(i)); } // show body content Log.i(response.body().string()); } });
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"); OkHttpClient client = new OkHttpClient(); String url = "http://www.ssaurel.com/tmp/jsonService"; String json = "{'mode' : 'test'}"; // Json Content ... RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // manage failure } @Override public void onResponse(Call call, Response response) throws IOException { // Manage response Log.i(response.body().string()); } });
Conclusion
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.
Leave a Reply
You must be logged in to post a comment.