Retrofit is a well known networking library among Android Developers, some even consider it as the industry standard. There are many reasons why it is so popular, great support for REST API’s, easily testable, easy to set up and make network calls. In this tutorial I will show you how to set up and use Retrofit to meet your app’s networking needs.

Setting up retrofit

Add the following dependency in your build.gradle  file.

We will using Gson to convert JSON into our POJO’s. Retrofit provides a dependency that will automatically convert JSON into POJO for us. Add the following dependency in your build.gradle file.

If your project doesn’t declare the Internet permission, then please do so immediately in your Manifest.

Now that we are done adding dependencies, lets write some code which is required to set up Retrofit.

Create a class named NetworkService .

This class will be a singleton, so declare a static variable and a function that assigns and returns the value of the variable of the same type. Please read about Singleton in Java if you don’t know about this pattern.

For testing purposes, we are using JSONPlaceholder which provides fake online REST API for developers.

Now we will declare and initialise Retrofit in the constructor of NetworkService.

The set up is complete, now we need to define the end points that will return some kind of data.

Adding end points

Create an interface named JSONPlaceHolderApi .

In the JSONPlaceHolder website /posts/id is the end point that returns the post with the corresponding ID. This end point takes a GET request and returns the JSON data in the following format:

First we will make a corresponding POJO for the JSON response.

As you can see this is a simple POJO class. We have annotated the variables with @SerializedName() and passed in a name, these passed names are the actual keys in the returned JSON data from api, so you are free to change the variable name to whatever you like but make sure the that name passed in SerializedName annotation is exactly what is present in JSON.

In the interface created above, define the end point with required parameters.

Since we are sending a GET  request, we have annotate the method with a @GET annotation, inside it we have passed the end point that we want to send the request to. You can see we haven’t added the complete URL, Retrofit will automatically take the BASE_URL that you passed in NetworkService class and append it with the remaining url.

The return type of method is Call<Post> . Call is a class provided by Retrofit itself which should be the return type of all the methods in the interface, Call is a generic class and takes the type of object we want the JSON to be converted to, we have passed Post  since that is what we want the JSON response to be converted to. In the parameters we have passed in an integer and annotated it with @Path in the path we have written id, what Retrofit will do is that the value passed as the parameter and replace it instead of the {id}  in the end point, so if we pass 1 as the parameter value, the end point generated will be /posts/1 , if we pass 10 it will become /posts/10 .

Now we want Retrofit to provide us with the implementation of our JSONPlaceHolderApi  interface. We do this by using the create()  method on Retrofit.

Now we need to get the JSONPlaceHolderApi from NetworkService and send the request.

The returned Call object has a method named enqueue that takes in a Callback<T> . In onResponse we get the result Response<Post> that contains our retuned Post object from server. We get the Post object by calling response.body() . Rest of the code is pretty self explanatory.

Never miss a post from TheTechnoCafe


Sending different types of request

JSONPlaceHolder Api has various end points that we can call.

Fetching list of Posts (GET)

When getting list of all posts, we changed the end point and also the return type of function.

Sending a query parameter

When you need to send a query parameter, all you need to do is annotate the promoter of function with @Query() .

So if we pass 6 as the value in method parameter, the end point generated will be /posts?userId=6

Sending a POST request

To send a POST  POST  request all you need to change is the method annotation.

In POST requests many times we need to send a body, we do that by just adding a method parameter and annotating it with @Body .Retrofit will use GSON to convert this object to JSON.

There are various types of Requests that you can send, convening all of them is out of scope of this article.

Intercepting Requests

Retrofit provides a way to intercept Requests and log them in Logcat. Lets set up the Interceptor and see its magic. Add the following dependency in the build.gradle file.

Update the NetworkService class and add the highlighted code.

Now whenever you send/receive a request, all of its details such as URL, Headers, Body will be logged.

That is it for this tutorial. You can find the code for this project on GitHub here.

A complete guide to integrating AdMob in your Android App.

How to use Room in Android.

Using Fonts in Android.

Understanding LiveData in Android Architecture Components.


Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!