Retrofit

Sithara Rao
4 min readFeb 21, 2020

This post will give you an overview of retrofit library and its uses.

What is Retrofit?

Retrofit is a REST client for Android and Java developed by Square. It helps us in performing the network operations easier and faster.

Using Retrofit, we can minimize writing the boiler plate code for performing network operations.

Procedure:

  1. An interface that has APIs
  2. A POJO class
  3. Retrofit instance
Retrofit

URL and segregation of it as Base URL and API

I have made use of dummy API ( from the website: http://dummy.restapiexample.com) to understand the concept much better with an example.

I am using https://dummy.restapiexample.com/api/v1/employees API for the reference.

The URL is divided into two: Base url and the API

As per the above URL, we can divide it into:

Base url: https://dummy.restapiexample.com/api/v1

API: /employees

The response looks something like this:

{
"status":"success",
"data":[
{
"id":"1",
"employee_name":"Tiger Nixon",
"employee_salary":"320800",
"employee_age":"61",
"profile_image":""
},
{
"id":"2",
"employee_name":"Garrett Winters",
"employee_salary":"170750",
"employee_age":"63",
"profile_image":""
},
{
"id":"3",
"employee_name":"Ashton Cox",
"employee_salary":"86000",
"employee_age":"66",
"profile_image":""
},
{
"id":"4",
"employee_name":"Cedric Kelly",
"employee_salary":"433060",
"employee_age":"22",
"profile_image":""
},
{
"id":"5",
"employee_name":"Airi Satou",
"employee_salary":"162700",
"employee_age":"33",
"profile_image":""
},
{
"id":"6",
"employee_name":"Brielle Williamson",
"employee_salary":"372000",
"employee_age":"61",
"profile_image":""
},
{
"id":"7",
"employee_name":"Herrod Chandler",
"employee_salary":"137500",
"employee_age":"59",
"profile_image":""
},
{
"id":"8",
"employee_name":"Rhona Davidson",
"employee_salary":"327900",
"employee_age":"55",
"profile_image":""
},
{
"id":"9",
"employee_name":"Colleen Hurst",
"employee_salary":"205500",
"employee_age":"39",
"profile_image":""
},
{
"id":"10",
"employee_name":"Sonya Frost",
"employee_salary":"103600",
"employee_age":"23",
"profile_image":""
},
{
"id":"11",
"employee_name":"Jena Gaines",
"employee_salary":"90560",
"employee_age":"30",
"profile_image":""
},
{
"id":"12",
"employee_name":"Quinn Flynn",
"employee_salary":"342000",
"employee_age":"22",
"profile_image":""
},
{
"id":"13",
"employee_name":"Charde Marshall",
"employee_salary":"470600",
"employee_age":"36",
"profile_image":""
},
{
"id":"14",
"employee_name":"Haley Kennedy",
"employee_salary":"313500",
"employee_age":"43",
"profile_image":""
},
{
"id":"15",
"employee_name":"Tatyana Fitzpatrick",
"employee_salary":"385750",
"employee_age":"19",
"profile_image":""
},
{
"id":"16",
"employee_name":"Michael Silva",
"employee_salary":"198500",
"employee_age":"66",
"profile_image":""
},
{
"id":"17",
"employee_name":"Paul Byrd",
"employee_salary":"725000",
"employee_age":"64",
"profile_image":""
},
{
"id":"18",
"employee_name":"Gloria Little",
"employee_salary":"237500",
"employee_age":"59",
"profile_image":""
},
{
"id":"19",
"employee_name":"Bradley Greer",
"employee_salary":"132000",
"employee_age":"41",
"profile_image":""
},
{
"id":"20",
"employee_name":"Dai Rios",
"employee_salary":"217500",
"employee_age":"35",
"profile_image":""
},
{
"id":"21",
"employee_name":"Jenette Caldwell",
"employee_salary":"345000",
"employee_age":"30",
"profile_image":""
},
{
"id":"22",
"employee_name":"Yuri Berry",
"employee_salary":"675000",
"employee_age":"40",
"profile_image":""
},
{
"id":"23",
"employee_name":"Caesar Vance",
"employee_salary":"106450",
"employee_age":"21",
"profile_image":""
},
{
"id":"24",
"employee_name":"Doris Wilder",
"employee_salary":"85600",
"employee_age":"23",
"profile_image":""
}
]
}

Add the dependency:

Before jumping into the coding part, we shall add the retrofit and gson dependencies in our gradle file:

implementation 'com.squareup.retrofit2:retrofit:latest_version'
implementation 'com.squareup.retrofit2:converter-gson:latest_version'

Follow the below procedure:

1. An interface that has API’s

As per the response, we can fetch the employee data and the status. So we shall create an interface with empty method getData() that returns the Employee data. This is a GET request, so we shall annotate it with @GET(“api_name”)

public interface ApiCaller {

String BASE_URL = "https://dummy.restapiexample.com/api/v1/";

@GET("employees")
Call<EmployeeData> getData();
}

2. A POJO class

Here we are using Gson to parse the JSON response to the POJO class that is internally carried out by Retrofit. One good thing about this is, the parsing is done automatically on background thread.

Employee Data class:

public class EmployeeData {
private Data[] data;

private String status;

public Data[] getData() {
return data;
}

public void setData(Data[] data) {
this.data = data;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}

Data class:

public class Data {

private String id;

@SerializedName("employee_name")
private String employeeName;

@SerializedName("employee_salary")
private String employeeSalary;


@SerializedName("employee_salary")
private String employeAge;


@SerializedName("profileImage")
private String profileImage;


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public String getEmployeeSalary() {
return employeeSalary;
}

public void setEmployeeSalary(String employeeSalary) {
this.employeeSalary = employeeSalary;
}

public String getEmployeAge() {
return employeAge;
}

public void setEmployeAge(String employeAge) {
this.employeAge = employeAge;
}

public String getProfileImage() {
return profileImage;
}

public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}
}

3. Retrofit instance

Instantiate the retrofit by setting the base url and converter.

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiCaller.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

Send the request

Once the retrofit object is created, reference to the API interface is created using retrofit’s object.

Call the API using API reference and enqueue it. This will asynchronously send the request to the server and notifies with a success/failure response.

The onResponse( ) and onFailure( ) callback controls are given to the main thread. Unlike volley, retrofit gives us the parsed response directly to the main thread without the developers writing a code to parse it.

ApiCaller apiCaller = retrofit.create(ApiCaller.class);Call<EmployeeData> employeeData = apiCaller.getData();employeeData.enqueue(new Callback<EmployeeData>() {
@Override
public void onResponse(Call<EmployeeData> call,
Response<EmployeeData> response) {
Log.d(TAG, "onResponse: "+response.body());
}

@Override
public void onFailure(Call<EmployeeData> call,
Throwable t) {
Log.e(TAG, "onFailure: "+t.getMessage());
}
});

For those who are wondering about “Call” and “Callback”, here’s the answer. Retrofit methods are invoked using the “Call” interface. The enqueue( ) method needs “Call” reference to send the request to server.

The enqueue method ask for “Callback” interface references to communicate the response back to the callee.

Please refer to the below link for better understanding of the code:

Stay tuned for updates! Happy coding 🙋🏻

--

--