// Tutorial //

Retrofit Android Example Tutorial

Published on August 3, 2022
Default avatar
By Anupam Chugh
Developer and author at DigitalOcean.
Retrofit Android Example Tutorial

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Welcome to Retrofit Android Example Tutorial. Today we’ll use the Retrofit library developed by Square to handle REST API calls in our android application.

Retrofit Android

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services. We’ll not go into the details of Retrofit 1.x versions and jump onto Retrofit 2 directly which has a lot of new features and a changed internal API compared to the previous versions. Retrofit 2 by default leverages OkHttp as the networking layer and is built on top of it. Retrofit automatically serialises the JSON response using a POJO(Plain Old Java Object) which must be defined in advanced for the JSON Structure. To serialise JSON we need a converter to convert it into Gson first. We need to add the following dependencies in our build.grade file.

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

OkHttp dependency is already shipped with Retrofit 2 dependency. If you wish to use a separate OkHttp dependency, you should exclude the OkHttp dependency from Retrofit 2 as:

compile ('com.squareup.retrofit2:retrofit:2.1.0') {  
  // exclude Retrofit’s OkHttp dependency module and define your own module import
  exclude module: 'okhttp'
}
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttps:3.4.1'
  • The logging-interceptor generates a log string of the entire response that’s returned.
  • There are other converters to parse the JSON to the necessary type. A few of them are listed below.
  1. Jackson : com.squareup.retrofit2:converter-jackson:2.1.0
  2. Moshi : com.squareup.retrofit2:converter-moshi:2.1.0
  3. Protobuf : com.squareup.retrofit2:converter-protobuf:2.1.0
  4. Wire : com.squareup.retrofit2:converter-wire:2.1.0
  5. Simple XML : com.squareup.retrofit2:converter-simplexml:2.1.0

Add the permission to access internet in the AndroidManifest.xml file.

OkHttp Interceptors

Interceptors are a powerful mechanism present in OkHttp that can monitor, rewrite, and retry calls. Interceptors can be majorly divided into two categories:

  • Application Interceptors : To register an application interceptor, we need to call addInterceptor() on OkHttpClient.Builder
  • Network Interceptors : To register a Network Interceptor, invoke addNetworkInterceptor() instead of addInterceptor()

Setting Up the Retrofit Interface

package com.journaldev.retrofitintro;

import com.journaldev.retrofitintro.pojo.MultipleResource;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

class APIClient {

    private static Retrofit retrofit = null;

    static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


        retrofit = new Retrofit.Builder()
                .baseUrl("https://reqres.in")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();



        return retrofit;
    }

}

The getClient() method in the above code will be called every time while setting up a Retrofit interface. Retrofit provides with a list of annotations for each of the HTTP methods: @GET, @POST, @PUT, @DELETE, @PATCH or @HEAD. Let’s see how our APIInterface.java class looks like.

package com.journaldev.retrofitintro;

import com.journaldev.retrofitintro.pojo.MultipleResource;
import com.journaldev.retrofitintro.pojo.User;
import com.journaldev.retrofitintro.pojo.UserList;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

interface APIInterface {

    @GET("/api/unknown")
    Call<MultipleResource> doGetListResources();

    @POST("/api/users")
    Call<User> createUser(@Body User user);

    @GET("/api/users?")
    Call<UserList> doGetUserList(@Query("page") String page);

    @FormUrlEncoded
    @POST("/api/users?")
    Call<UserList> doCreateUserWithField(@Field("name") String name, @Field("job") String job);
}

In the above class, we’ve defined some methods that perform HTTP requests with annotation. We’ve used a few test APIs from here @GET("/api/unknown") calls doGetListResources();. doGetListResources() is the method name. MultipleResource.java is a Model POJO class for our response object that’s used to map the response parameters to their respective variables. These POJO class act as the method return type. A simple POJO class for MultipleResources.java is given below.

package com.journaldev.retrofitintro.pojo;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class MultipleResource {

    @SerializedName("page")
    public Integer page;
    @SerializedName("per_page")
    public Integer perPage;
    @SerializedName("total")
    public Integer total;
    @SerializedName("total_pages")
    public Integer totalPages;
    @SerializedName("data")
    public List<Datum> data = null;

    public class Datum {

        @SerializedName("id")
        public Integer id;
        @SerializedName("name")
        public String name;
        @SerializedName("year")
        public Integer year;
        @SerializedName("pantone_value")
        public String pantoneValue;

    }
}

@SerializedName annotation is used to specify the name of the field that’s in the JSON Response. To create a POJO class for each response, we can go to https://www.jsonschema2pojo.org/ and paste the json response structure as shown in the image below. retrofit android example tutorial json schema Preview the POJO class and copy it into your Android Studio Project Structure. The POJO classes are wrapped into a typed Retrofit Call class. Note: A JSONArray is serialised a List of Objects in the POJO classes Method Parameters : There are a wide variety of possible options of parameters to pass inside a method:

  • @Body - Sends Java objects as request body.
  • @Url - use dynamic URLs.
  • @Query - We can simply add a method parameter with @Query() and a query parameter name, describing the type. To URL encode a query use the form: @Query(value = "auth_token",encoded = true) String auth_token
  • @Field - send data as form-urlencoded. This requires a @FormUrlEncoded annotation attached with the method. The @Field parameter works only with a POST

Note: @Field requires a mandatory parameter. In cases when @Field is optional, we can use @Query instead and pass a null value.

Retrofit Android Example Project Structure

android retrofit example tutorial, retrofit 2 tutorial The pojo package defines four model classes for each of the API endpoint responses defined in the APIInterface.java class. User.java

package com.journaldev.retrofitintro.pojo;

import com.google.gson.annotations.SerializedName;

public class User {

    @SerializedName("name")
    public String name;
    @SerializedName("job")
    public String job;
    @SerializedName("id")
    public String id;
    @SerializedName("createdAt")
    public String createdAt;

    public User(String name, String job) {
        this.name = name;
        this.job = job;
    }


}

The above class is used to create the Response Body for the createUser() method UserList.java

package com.journaldev.retrofitintro.pojo;

import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class UserList {

    @SerializedName("page")
    public Integer page;
    @SerializedName("per_page")
    public Integer perPage;
    @SerializedName("total")
    public Integer total;
    @SerializedName("total_pages")
    public Integer totalPages;
    @SerializedName("data")
    public List<Datum> data = new ArrayList();

    public class Datum {

        @SerializedName("id")
        public Integer id;
        @SerializedName("first_name")
        public String first_name;
        @SerializedName("last_name")
        public String last_name;
        @SerializedName("avatar")
        public String avatar;

    }
}

CreateUserResponse.java

package com.journaldev.retrofitintro.pojo;

import com.google.gson.annotations.SerializedName;

public class CreateUserResponse {

    @SerializedName("name")
    public String name;
    @SerializedName("job")
    public String job;
    @SerializedName("id")
    public String id;
    @SerializedName("createdAt")
    public String createdAt;
}

The MainActivity.java is where we call each of the API endpoints defined in the Interface class and display each of the fields in a Toast/TextView.

package com.journaldev.retrofitintro;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.journaldev.retrofitintro.pojo.CreateUserResponse;
import com.journaldev.retrofitintro.pojo.MultipleResource;
import com.journaldev.retrofitintro.pojo.User;
import com.journaldev.retrofitintro.pojo.UserList;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    TextView responseText;
    APIInterface apiInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        responseText = (TextView) findViewById(R.id.responseText);
        apiInterface = APIClient.getClient().create(APIInterface.class);


        /**
         GET List Resources
         **/
        Call<MultipleResource> call = apiInterface.doGetListResources();
        call.enqueue(new Callback<MultipleResource>() {
            @Override
            public void onResponse(Call<MultipleResource> call, Response<MultipleResource> response) {


                Log.d("TAG",response.code()+"");

                String displayResponse = "";

                MultipleResource resource = response.body();
                Integer text = resource.page;
                Integer total = resource.total;
                Integer totalPages = resource.totalPages;
                List<MultipleResource.Datum> datumList = resource.data;

                displayResponse += text + " Page\n" + total + " Total\n" + totalPages + " Total Pages\n";

                for (MultipleResource.Datum datum : datumList) {
                    displayResponse += datum.id + " " + datum.name + " " + datum.pantoneValue + " " + datum.year + "\n";
                }

                responseText.setText(displayResponse);

            }

            @Override
            public void onFailure(Call<MultipleResource> call, Throwable t) {
                call.cancel();
            }
        });

        /**
         Create new user
         **/
        User user = new User("morpheus", "leader");
        Call<User> call1 = apiInterface.createUser(user);
        call1.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user1 = response.body();

                Toast.makeText(getApplicationContext(), user1.name + " " + user1.job + " " + user1.id + " " + user1.createdAt, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                call.cancel();
            }
        });

        /**
         GET List Users
         **/
        Call<UserList> call2 = apiInterface.doGetUserList("2");
        call2.enqueue(new Callback<UserList>() {
            @Override
            public void onResponse(Call<UserList> call, Response<UserList> response) {

                UserList userList = response.body();
                Integer text = userList.page;
                Integer total = userList.total;
                Integer totalPages = userList.totalPages;
                List<UserList.Datum> datumList = userList.data;
                Toast.makeText(getApplicationContext(), text + " page\n" + total + " total\n" + totalPages + " totalPages\n", Toast.LENGTH_SHORT).show();

                for (UserList.Datum datum : datumList) {
                    Toast.makeText(getApplicationContext(), "id : " + datum.id + " name: " + datum.first_name + " " + datum.last_name + " avatar: " + datum.avatar, Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onFailure(Call<UserList> call, Throwable t) {
                call.cancel();
            }
        });


        /**
         POST name and job Url encoded.
         **/
        Call<UserList> call3 = apiInterface.doCreateUserWithField("morpheus","leader");
        call3.enqueue(new Callback<UserList>() {
            @Override
            public void onResponse(Call<UserList> call, Response<UserList> response) {
                UserList userList = response.body();
                Integer text = userList.page;
                Integer total = userList.total;
                Integer totalPages = userList.totalPages;
                List<UserList.Datum> datumList = userList.data;
                Toast.makeText(getApplicationContext(), text + " page\n" + total + " total\n" + totalPages + " totalPages\n", Toast.LENGTH_SHORT).show();

                for (UserList.Datum datum : datumList) {
                    Toast.makeText(getApplicationContext(), "id : " + datum.id + " name: " + datum.first_name + " " + datum.last_name + " avatar: " + datum.avatar, Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<UserList> call, Throwable t) {
                call.cancel();
            }
        });

    }
}

apiInterface = APIClient.getClient().create(APIInterface.class); is used to instantiate the APIClient. To map the Model class to the response we use: MultipleResource resource = response.body(); Running the application would call each of the endpoints and display a Toast message for them accordingly. This brings an end to Retrofit android example tutorial. You can download the Android Retrofit example project from the link below.

Download Retrofit Android Example Project


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

package com.hk.crudretrofit; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class RetrofitClient { private static String BASE_URL=“http://192.168.43.163:8080/UserApi/”; private static RetrofitClient retrofitClient; private static Retrofit retrofit; private OkHttpClient.Builder builder = new OkHttpClient.Builder(); private HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); public RetrofitClient() { Gson gson = new GsonBuilder() .setLenient() .create(); interceptor.level(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(interceptor); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(builder.build()) .build(); } public static synchronized RetrofitClient getInstance(){ if(retrofitClient==null){ retrofitClient=new RetrofitClient(); } return retrofitClient; } public Api getApi(){ return retrofit.create(Api.class); } }

- hk

    pls help me, how can i save into sqlite

    - rajna

      A utilização desse projeto me ajudou muito a conseguir realizar meu @GET, muito obrigado.

      - BarbosaHub

        Requset {“userId”:“435456”} Reaponse “mesage”

        - sugandha jha

          Hi anupam, Thank u so much for this tutorial. I always use this for my retrofit related queries and recommend this to others also. But i noticed a mistake in the code u have given to write in build.gradle compile ‘com.squareup.okhttp3:okhttps:3.4.1’ there is no okhttps in that com.squareup.okhttp3… It’s okhttp:3.14.1(Now latest version is 14). Earlier i didn’t get that error. But last time when i used it i got error in syncing. And i didn’t know why? Only last day i found the reason.

          - Devu Mani

            Please decrease the size of your dashboard

            - Anmol

              Hello. POST code in your code seems to be wrong. Isn’t it right to do this? /** POST name and job Url encoded. **/ Call call3 = apiInterface.doCreateUserWithField(“morpheus”,“leader”); call3.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { CreateUserResponse createUserResponse = response.body(); String name = createUserResponse.name; String job = createUserResponse.job; String id = createUserResponse.id; String createdAt = createUserResponse.createdAt; Toast.makeText( RestApiTestActivity.this, name +" name\n" + job + " job\n" + id + " id\n" + createdAt, Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call call, Throwable t) { call.cancel(); } });

              - sharpen

                Hi, I have one query? if i am searching API implement in project using retrofit, that using time multiple hit to server on key press. So my problem is , that will not working properly.

                - Tejas

                  Hii , is there any way to check the parameters passed to the api? Because when I try to call api with post annotation and passing a pojo class object ,the api cannot read the parameters.

                  - Neha

                    how do we tell or know when retrofit that it gets to the end of a json file ?

                    - CHIDOZIE UGBOMA