Build An Efficient REST Network Client In Android (With Retrofit 2/OkHttp 3) - Part I - Retrofit - DuckMa

Build An Efficient REST Network Client In Android (With Retrofit 2/OkHttp 3) – Part I – Retrofit

Build An Efficient REST Network Client In Android

Blog RestAndroid DuckMa

In this three-part series (plus a bonus at the end!) I will show you how to design an efficient network communication with REST web services for Android.

We’ll provide examples on how to fetch data efficiently from a REST web service with Square Retrofit, Square OkHttp, and HTTP/2 protocol (hosted in Google Cloud Platform), implementing a two-level cache using OkHttp and Realm.

Introduction: How to create a simple project in Google AppEngine that return a static Json
Part 1: Retrofit
Part II: HTTP/2 with OkHttp
Part III: Caching

Part I – Retrofit

Note: In this article we refer to Retrofit 2.0, which just passed its stage to Beta 3 with support for OkHttp 3, and has been publicly launched and available to everyone. (Changelog here.)

Retrofit is a library used to implement a REST client in Android by Square, and is now one of the most popular HTTP Client libraries for Android. It makes it relatively easy to retrieve and upload JSON or other structured data to a REST-based web service.

Annotations are used in Retrofit to describe the HTTP request:

  • URL parameter replacement and query parameter support
  • Object conversion to request body (e.g., JSON, protocol buffers)
  • Multipart request body and file upload

Retrofit uses the OkHttp library for HTTP requests.

By default, Retrofit can only deserialize HTTP bodies into OkHttp’s ResponseBody but can be configured with converters, support libraries that provide interpreters for data serialization.

Typically for JSON you use Gson, but you can add custom converters to process XML or other protocols.

Retrofit allows you to use the following converters:

  • Gson: com.squareup.retrofit:converter-gson
  • Jackson: com.squareup.retrofit:converter-jackson
  • Moshi: com.squareup.retrofit:converter-moshi
  • Protobuf: com.squareup.retrofit:converter-protobuf
  • Wire: com.squareup.retrofit:converter-wire
  • Simple XML: com.squareup.retrofit:converter-simplexml

To work with Retrofit you need basically three classes:
Model class, which is used to map the JSON data to Java Objects.
Interfaces, which defines the possible HTTP operations.
Retrofit.Builder class, which uses the interface Builder API. This allows you to define the URL endpoint for the HTTP operation.

(You can find the code of this example here:

Try to decode this URL ( and display a simple list with the content parsed from the contained JSON.

After the creation of a new app, you have to add to your build with Gradle the dependencies for Retrofit:

compile ‘com.squareup.retrofit2:retrofit:2.0.0-beta3’
compile ‘com.squareup.retrofit2:converter-gson:2.0.0-beta3’

And the proper permissions in Manifest.

Then you have to declare your Models and the Retrofit Response Model (the model of the response).

In this case you have only one simple model:

public class Content {
String id;
String title;
String image;

public class ContentResponse {
String title;
String type;
double version;
List contents;

Then you have to implement the interfaces of Retrofit (because Retrofit turns your HTTP API into a Java interface)

public interface AppAPI {
Call loadContents();

After this we have to move to the Main Activity and implement the logic of the call. (Here I have ommitted all the UI. You can check out the code with the UI and all the classes implemented until now here:

The list is based on the Android RecyclerView. (Check out the tutorial “Android RecyclerView – A Brief Introduction,” if you want to know how it has been done.)

Now the Main Activity implements the Retrofit Callback and overridse the required methods.

public class MainActivity extends AppCompatActivity implements Callback
public void onResponse(Response response) {}

public void onFailure(Throwable t) {}

After that you have to declare Retrofit through the Retrofit.Builder class, prepare and execute the call.

Retrofit retrofit = new Retrofit.Builder()
// prepare call in Retrofit 2.0
AppAPI appApi = retrofit.create(AppAPI.class);
Call call = appApi.loadContents();

and at the end handle the Successful (or Unsuccessful) Response in the two callbacks defined above.

Here’s the result:
(You can find the code of this example here:

Star Wars Demo