Retrofit Android Example Tutorial

Filed Under: Android

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:okhttp: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 http://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.

Comments

  1. Neha says:

    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.

  2. CHIDOZIE UGBOMA says:

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

    1. Anupam says:

      Why would you want to know that?

  3. Zahid Gulbaz says:

    Hello
    how i can bind the integer value to textview.

    {
    “level”: [
    {
    “id”: 112,
    “level_name”: “Matric”,
    “created_at”: null,
    “updated_at”: null
    }
    ]
    }

  4. Jayant Bhave says:

    Hello there, I notice that APIClient declares Retrofit object and getClient method as static.
    And there is only one instance of APIInterface in MainActivity.java. As long as we have only one APIInterface object in an application, then its simulating singleton pattern

    When we instantiate second object e.g. say apiInterface2, its not the same as apiInterface which means this is not a singleton class.

    How do we implement truly singleton pattern here even for APIInterface?

    1. Anupam says:

      Create a getInstance synchronized method which returns the instance of retrofit.
      But this can lead to performance issues. Synchronised is an expensive option in multi threading.

  5. Jayant Bhave says:

    @Jayant Bhave
    .execute method will throw networkOnMainThread exception. If we want to make subsequent calls only if previous call succeeds then here is how to make these calls appear to be synchronous. Move the subsequent calls to onResponse method of previous call. Calls are still asynchronous but result will be synchronous.

    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);
    responseText.setMovementMethod(new ScrollingMovementMethod());
    apiInterface = APIClient.getClient().create(APIInterface.class);//getClient returns Retrofit object and create returns Interface object

    MakeCallRequest();

    }

    /**
    GET List Resources
    **/
    public void MakeCallRequest(){
    Call call = apiInterface.doGetListResources();
    call.enqueue(new Callback() {
    @Override
    public void onResponse(Call call, Response 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 datumList = resource.data;

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

    responseText.append(“\n***************************************************\n” +”Call doGetListResources” + “\n” );

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

    responseText.append(displayResponse);

    MakeCall1Request();

    }

    @Override
    public void onFailure(Call call, Throwable t) {
    responseText.append(“Call doGetListResources FAILED ****”);//Added by Jayant
    call.cancel();
    }
    });
    }

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

    Toast.makeText(getApplicationContext(), user1.name + ” ” + user1.job + ” ” + user1.id + ” ” + user1.createdAt, Toast.LENGTH_LONG).show();
    String strCall1Response = user1.name + ” ” + user1.job + ” ” + user1.id + ” ” + user1.createdAt;
    responseText.append(“\n***************************************************\n” +”Call1 createUser” + “\n”);//Added by Jayant
    responseText.append(strCall1Response);//Added by Jayant
    MakeCall2Request();
    }

    @Override
    public void onFailure(Call call, Throwable t) {
    responseText.append(“Call1 createUser FAILED ****”);//Added by Jayant
    call.cancel();
    }
    });
    }

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

    UserList userList = response.body();
    Integer text = userList.page;
    Integer total = userList.total;
    Integer totalPages = userList.totalPages;
    List datumList = userList.data;
    Toast.makeText(getApplicationContext(), text + ” page\n” + total + ” total\n” + totalPages + ” totalPages\n”, Toast.LENGTH_LONG).show();

    responseText.append(“\n***************************************************\n” + “Call2 doGetUserList” + “\n”);//Added by Jayant

    for (UserList.Datum datum : datumList) {
    Toast.makeText(getApplicationContext(), “id : ” + datum.id + ” name: ” + datum.first_name + ” ” + datum.last_name + ” avatar: ” + datum.avatar, Toast.LENGTH_LONG).show();
    String strCall2Response = “id : ” + datum.id + ” name: ” + datum.first_name + ” ” + datum.last_name + ” avatar: ” + datum.avatar + “\n”;
    responseText.append(strCall2Response);//Added by Jayant
    }
    MakeCall3Request();

    }

    @Override
    public void onFailure(Call call, Throwable t) {
    responseText.append(“Call2 doGetUserList FAILED ****”);//Added by Jayant
    call.cancel();
    }
    });
    }

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

    responseText.append(“\n***************************************************\n” + “Call3 doCreateUserWithField” + “\n”);//Added by Jayant

    for (UserList.Datum datum : datumList) {
    Toast.makeText(getApplicationContext(), “id : ” + datum.id + ” name: ” + datum.first_name + ” ” + datum.last_name + ” avatar: ” + datum.avatar, Toast.LENGTH_LONG).show();
    String strCall3Response = “id : ” + datum.id + ” name: ” + datum.first_name + ” ” + datum.last_name + ” avatar: ” + datum.avatar;
    responseText.append(strCall3Response);
    }

    }

    @Override
    public void onFailure(Call call, Throwable t) {
    responseText.append(“Call3 doCreateUserWithField FAILED ****”);//Added by Jayant
    call.cancel();
    }
    });

    }
    }

  6. Jayant Bhave says:

    Hello there,
    Its a great example post on retrofit.

    I notice that, the response to the call, call1, call2 and call3 come in random order even if they were called in the sequence call, call1, call2 and call3.

    How to make them blocking i.e. not to invoke call2 before call1 finishes?

  7. itsme says:

    thanks for the post.. really helpful

  8. gaurav meghanathi says:

    step 1
    public class ApiClient {
    private static Retrofit retrofit = null;

    private static Gson gson = new GsonBuilder()
    .setLenient()
    .create();

    public static Retrofit getClient(String baseUrl) {
    if(retrofit == null) {
    retrofit = new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build();
    }

    return retrofit;
    }
    }

    ————————————————————————————-
    step 2
    public class ApiServices {

    public static final String BASE_URL = “http://xyz.com/api/”;

    public static AccountService getAccountService() {
    return ApiClient.getClient(BASE_URL).create(AccountService.class);
    }

    }
    —————————————————————————————
    step 3
    make an interface public class

    public interface AccountService {

    @POST(“account_register”)
    Call doRegister (@Body account.register.Request registerRequest);

    @POST(“account_login”)
    Call doLogin(@Body Request loginRequest);

    @POST(“account_logout”)
    Call doLogOut(@Bodyaccount.profile.Request logoutRequest);

    @Multipart
    @POST(“account_profile_upload-profile”)
    Call doUploadImage(@Part MultipartBody.Part file, @Part(“user_id”) RequestBody userId, @Part(“sort_order”) RequestBody picOrder);

    @GET(“account_profile_upload-profile/{user_id}”)
    Call doGetProfileImages(@Path(“user_id”) int user_id);

    @DELETE(“account_profile_upload-profile/id/{id}”+”/sort_order/{sort_order}”)
    Call doRemoveImage(@Path(“id”) int user_id,@Path(“sort_order”)String sort_order);

    }
    ————————————————————————————————–
    step 4

    call retrofit on main activity
    private AccountService accountService;;

    accountService = ApiServices.getAccountService();

    accountService.doLogin(loginRequest).enqueue(new Callback() {
    @Override
    public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) {
    if (response.isSuccessful() && response.body()!=null) {
    switch (response.body().getType()) {
    case “failure”:
    try {
    edtEmailid.setError(response.body().getErrors().getEmail());
    edtPassword.setError(response.body().getErrors().getPassword());
    updateLoader(false);
    } catch (Exception e) {
    e.printStackTrace();
    }

    break;
    case “success”:
    try {
    model = response.body().getData();
    preference.createLoginSession(response.body().getData());
    } catch (JSONException e) {
    e.printStackTrace();
    }

    updateLoader(false);
    goToMeetUpActivity();

    break;
    }
    } else {
    Toast.makeText(getApplicationContext(), R.string.alert_api_failure, Toast.LENGTH_LONG).show();
    }
    }

    @Override
    public void onFailure(@NonNull Call call, @NonNull Throwable t) {
    Toast.makeText(getApplicationContext(), R.string.alert_api_failure, Toast.LENGTH_LONG).show();
    updateLoader(false);
    }
    });

  9. Binimol Benny says:

    how can i write the code for handling error responses

  10. Deep Rathod says:

    Hello there

    I try to pass data in PUT annotation but it gives me an error like method not allowed. so, How can i pass data in @PUT.

    Help me to solve this problem as soon as possible

  11. karthi says:

    hai there, i like to know that whether we can able to insert data to sqlite directly and only fetch data in the listview. if it is possible please help to know how?

  12. ZloZlo says:

    Tried with another site and the response.body() is NULL.
    What does that mean?

  13. ashish says:

    how to get response return from api into other method

  14. Ronaldo says:

    Thank you for show us a good example. I have a question, Do I need ‘com.google.code.gson:gson:2.6.2’ and ‘com.squareup.retrofit2:converter-gson:2.1.0’ together? I mean, in what case I need just one or both. what does gson do? and what does converter-gson do?.
    Thank you very much.

    1. Anupam says:

      gson is an open source java api for parsing and building json. It has extensive support for Java generics. It also provides support for converting third-party classes to JSON.
      converter-gson is a Retrofit converter used for JSON mapping.

      1. Ronaldo says:

        Thank you very much!

    2. shital says:

      gson is an open source java api for parsing and building json. It has extensive support for Java generics. It also provides support for converting third-party classes to JSON.
      converter-gson is a Retrofit converter used for JSON mapping.

  15. willy says:

    Thanks for your excellent tutorial.

    In MultipleResource.java, there’s a bit lack of code which leads to error.

    public List data = null;

    Should be

    public List data = null;

    1. Anupam says:

      It’s List<T> data = null;
      Perhaps formatting issue on wordpress. Check the source code!

  16. Abhishek Dhyani says:

    Really helpful post. Finally I got It.

    1. Anupam says:

      Copy pasting the api response in http://www.jsonschema2pojo.org/ gives the following POJO classes.

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class BeenHere {

      @SerializedName(“count”)
      @Expose
      public Integer count;
      @SerializedName(“marked”)
      @Expose
      public Boolean marked;
      @SerializedName(“lastCheckinExpiredAt”)
      @Expose
      public Integer lastCheckinExpiredAt;

      }
      ———————————–com.example.Category.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Category {

      @SerializedName(“id”)
      @Expose
      public String id;
      @SerializedName(“name”)
      @Expose
      public String name;
      @SerializedName(“pluralName”)
      @Expose
      public String pluralName;
      @SerializedName(“shortName”)
      @Expose
      public String shortName;
      @SerializedName(“icon”)
      @Expose
      public Icon icon;
      @SerializedName(“primary”)
      @Expose
      public Boolean primary;

      }
      ———————————–com.example.Contact.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Contact {

      @SerializedName(“phone”)
      @Expose
      public String phone;
      @SerializedName(“formattedPhone”)
      @Expose
      public String formattedPhone;
      @SerializedName(“twitter”)
      @Expose
      public String twitter;

      }
      ———————————–com.example.Entity.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Entity {

      @SerializedName(“indices”)
      @Expose
      public List indices = null;
      @SerializedName(“type”)
      @Expose
      public String type;

      }
      ———————————–com.example.Example.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Example {

      @SerializedName(“meta”)
      @Expose
      public Meta meta;
      @SerializedName(“response”)
      @Expose
      public Response response;

      }
      ———————————–com.example.Filter.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Filter {

      @SerializedName(“name”)
      @Expose
      public String name;
      @SerializedName(“key”)
      @Expose
      public String key;

      }
      ———————————–com.example.Group.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Group {

      @SerializedName(“type”)
      @Expose
      public String type;
      @SerializedName(“name”)
      @Expose
      public String name;
      @SerializedName(“items”)
      @Expose
      public List items = null;

      }
      ———————————–com.example.HereNow.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class HereNow {

      @SerializedName(“count”)
      @Expose
      public Integer count;
      @SerializedName(“summary”)
      @Expose
      public String summary;
      @SerializedName(“groups”)
      @Expose
      public List groups = null;

      }
      ———————————–com.example.Hours.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Hours {

      @SerializedName(“isOpen”)
      @Expose
      public Boolean isOpen;
      @SerializedName(“isLocalHoliday”)
      @Expose
      public Boolean isLocalHoliday;
      @SerializedName(“status”)
      @Expose
      public String status;

      }
      ———————————–com.example.Icon.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Icon {

      @SerializedName(“prefix”)
      @Expose
      public String prefix;
      @SerializedName(“suffix”)
      @Expose
      public String suffix;

      }
      ———————————–com.example.Item.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Item {

      @SerializedName(“reasons”)
      @Expose
      public Reasons reasons;
      @SerializedName(“venue”)
      @Expose
      public Venue venue;
      @SerializedName(“tips”)
      @Expose
      public List tips = null;
      @SerializedName(“referralId”)
      @Expose
      public String referralId;

      }
      ———————————–com.example.Item_.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Item_ {

      @SerializedName(“summary”)
      @Expose
      public String summary;
      @SerializedName(“type”)
      @Expose
      public String type;
      @SerializedName(“reasonName”)
      @Expose
      public String reasonName;

      }
      ———————————–com.example.LabeledLatLng.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class LabeledLatLng {

      @SerializedName(“label”)
      @Expose
      public String label;
      @SerializedName(“lat”)
      @Expose
      public Float lat;
      @SerializedName(“lng”)
      @Expose
      public Float lng;

      }
      ———————————–com.example.Likes.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Likes {

      @SerializedName(“count”)
      @Expose
      public Integer count;
      @SerializedName(“groups”)
      @Expose
      public List groups = null;
      @SerializedName(“summary”)
      @Expose
      public String summary;

      }
      ———————————–com.example.Location.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Location {

      @SerializedName(“address”)
      @Expose
      public String address;
      @SerializedName(“lat”)
      @Expose
      public Float lat;
      @SerializedName(“lng”)
      @Expose
      public Float lng;
      @SerializedName(“labeledLatLngs”)
      @Expose
      public List labeledLatLngs = null;
      @SerializedName(“postalCode”)
      @Expose
      public String postalCode;
      @SerializedName(“cc”)
      @Expose
      public String cc;
      @SerializedName(“city”)
      @Expose
      public String city;
      @SerializedName(“state”)
      @Expose
      public String state;
      @SerializedName(“country”)
      @Expose
      public String country;
      @SerializedName(“formattedAddress”)
      @Expose
      public List formattedAddress = null;
      @SerializedName(“crossStreet”)
      @Expose
      public String crossStreet;

      }
      ———————————–com.example.Meta.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Meta {

      @SerializedName(“code”)
      @Expose
      public Integer code;
      @SerializedName(“requestId”)
      @Expose
      public String requestId;

      }
      ———————————–com.example.Ne.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Ne {

      @SerializedName(“lat”)
      @Expose
      public Float lat;
      @SerializedName(“lng”)
      @Expose
      public Float lng;

      }
      ———————————–com.example.Photo.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Photo {

      @SerializedName(“prefix”)
      @Expose
      public String prefix;
      @SerializedName(“suffix”)
      @Expose
      public String suffix;

      }
      ———————————–com.example.Photo_.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Photo_ {

      @SerializedName(“id”)
      @Expose
      public String id;
      @SerializedName(“createdAt”)
      @Expose
      public Integer createdAt;
      @SerializedName(“source”)
      @Expose
      public Source source;
      @SerializedName(“prefix”)
      @Expose
      public String prefix;
      @SerializedName(“suffix”)
      @Expose
      public String suffix;
      @SerializedName(“width”)
      @Expose
      public Integer width;
      @SerializedName(“height”)
      @Expose
      public Integer height;
      @SerializedName(“visibility”)
      @Expose
      public String visibility;

      }
      ———————————–com.example.Photos.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Photos {

      @SerializedName(“count”)
      @Expose
      public Integer count;
      @SerializedName(“groups”)
      @Expose
      public List groups = null;

      }
      ———————————–com.example.Price.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Price {

      @SerializedName(“tier”)
      @Expose
      public Integer tier;
      @SerializedName(“message”)
      @Expose
      public String message;
      @SerializedName(“currency”)
      @Expose
      public String currency;

      }
      ———————————–com.example.Reasons.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Reasons {

      @SerializedName(“count”)
      @Expose
      public Integer count;
      @SerializedName(“items”)
      @Expose
      public List items = null;

      }
      ———————————–com.example.Response.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Response {

      @SerializedName(“suggestedFilters”)
      @Expose
      public SuggestedFilters suggestedFilters;
      @SerializedName(“headerLocation”)
      @Expose
      public String headerLocation;
      @SerializedName(“headerFullLocation”)
      @Expose
      public String headerFullLocation;
      @SerializedName(“headerLocationGranularity”)
      @Expose
      public String headerLocationGranularity;
      @SerializedName(“query”)
      @Expose
      public String query;
      @SerializedName(“totalResults”)
      @Expose
      public Integer totalResults;
      @SerializedName(“suggestedBounds”)
      @Expose
      public SuggestedBounds suggestedBounds;
      @SerializedName(“groups”)
      @Expose
      public List groups = null;

      }
      ———————————–com.example.Source.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Source {

      @SerializedName(“name”)
      @Expose
      public String name;
      @SerializedName(“url”)
      @Expose
      public String url;

      }
      ———————————–com.example.Stats.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Stats {

      @SerializedName(“checkinsCount”)
      @Expose
      public Integer checkinsCount;
      @SerializedName(“usersCount”)
      @Expose
      public Integer usersCount;
      @SerializedName(“tipCount”)
      @Expose
      public Integer tipCount;

      }
      ———————————–com.example.SuggestedBounds.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class SuggestedBounds {

      @SerializedName(“ne”)
      @Expose
      public Ne ne;
      @SerializedName(“sw”)
      @Expose
      public Sw sw;

      }
      ———————————–com.example.SuggestedFilters.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class SuggestedFilters {

      @SerializedName(“header”)
      @Expose
      public String header;
      @SerializedName(“filters”)
      @Expose
      public List filters = null;

      }
      ———————————–com.example.Sw.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Sw {

      @SerializedName(“lat”)
      @Expose
      public Float lat;
      @SerializedName(“lng”)
      @Expose
      public Float lng;

      }
      ———————————–com.example.Tip.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Tip {

      @SerializedName(“id”)
      @Expose
      public String id;
      @SerializedName(“createdAt”)
      @Expose
      public Integer createdAt;
      @SerializedName(“text”)
      @Expose
      public String text;
      @SerializedName(“entities”)
      @Expose
      public List entities = null;
      @SerializedName(“type”)
      @Expose
      public String type;
      @SerializedName(“canonicalUrl”)
      @Expose
      public String canonicalUrl;
      @SerializedName(“logView”)
      @Expose
      public Boolean logView;
      @SerializedName(“agreeCount”)
      @Expose
      public Integer agreeCount;
      @SerializedName(“disagreeCount”)
      @Expose
      public Integer disagreeCount;
      @SerializedName(“todo”)
      @Expose
      public Todo todo;
      @SerializedName(“user”)
      @Expose
      public User user;
      @SerializedName(“likes”)
      @Expose
      public Likes likes;
      @SerializedName(“photo”)
      @Expose
      public Photo_ photo;
      @SerializedName(“photourl”)
      @Expose
      public String photourl;

      }
      ———————————–com.example.Todo.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Todo {

      @SerializedName(“count”)
      @Expose
      public Integer count;

      }
      ———————————–com.example.User.java———————————–

      package com.example;

      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class User {

      @SerializedName(“id”)
      @Expose
      public String id;
      @SerializedName(“firstName”)
      @Expose
      public String firstName;
      @SerializedName(“lastName”)
      @Expose
      public String lastName;
      @SerializedName(“gender”)
      @Expose
      public String gender;
      @SerializedName(“photo”)
      @Expose
      public Photo photo;

      }
      ———————————–com.example.Venue.java———————————–

      package com.example;

      import java.util.List;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;

      public class Venue {

      @SerializedName(“id”)
      @Expose
      public String id;
      @SerializedName(“name”)
      @Expose
      public String name;
      @SerializedName(“contact”)
      @Expose
      public Contact contact;
      @SerializedName(“location”)
      @Expose
      public Location location;
      @SerializedName(“categories”)
      @Expose
      public List categories = null;
      @SerializedName(“verified”)
      @Expose
      public Boolean verified;
      @SerializedName(“stats”)
      @Expose
      public Stats stats;
      @SerializedName(“rating”)
      @Expose
      public Float rating;
      @SerializedName(“ratingSignals”)
      @Expose
      public Integer ratingSignals;
      @SerializedName(“allowMenuUrlEdit”)
      @Expose
      public Boolean allowMenuUrlEdit;
      @SerializedName(“beenHere”)
      @Expose
      public BeenHere beenHere;
      @SerializedName(“hours”)
      @Expose
      public Hours hours;
      @SerializedName(“photos”)
      @Expose
      public Photos photos;
      @SerializedName(“hereNow”)
      @Expose
      public HereNow hereNow;
      @SerializedName(“venueRatingBlacklisted”)
      @Expose
      public Boolean venueRatingBlacklisted;
      @SerializedName(“url”)
      @Expose
      public String url;
      @SerializedName(“price”)
      @Expose
      public Price price;

      }

      Remove expose annotations and use them and you’ll be good!

  17. Ahsan Saeed says:

    Hey.. I got this type of Error. in forEach loop.. in every ForEach loop..
    Error:
    java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.android.retrofitjournaldevcom.MultipleResource$Datum

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

    Help me to solve this problem as soon as possible

    1. Lita Nurlaelati says:

      I also get it. What should I do?

      1. Anupam says:

        Guys there was a glitch in the formatting wherein the angular brackets weren’t visible. Have fixed it.
        Besides, the full source code available for download works fine.

        Thanks

  18. el says:

    The app keeps closing down on start on Android studio 2.3.3
    Can you please assist.

    1. Avnish says:

      You are right

  19. Aalap Patel says:

    TO convert the response in POJO form the website gives error saying “There’s a problem: Unexpected character (‘p’ (code 112)): was expecting double-quote to start field name (line 2, column 2)”.
    So I manually apply double quotes to all the keys of the json response.
    What is the alternative for that ? How did u manage to get double quotes on all the keys with out manually applying it ?

    1. Annonymous says:

      check your responce from json api then copy it to pojo if stil then check limit if averege if issue still upload your respoce here or your api

  20. Ankit Palli says:

    Hey Thank you for such a good example and detailed explaination about Retrofit

    After my complete day struggle finally got my issue solved…

    Thank you very much.

    1. Anupam says:

      Glad that your issues were resolved Ankit. If you want me to cover any topics feel free to comment below.

  21. Ankit Palli says:

    Hey, Anupam Chugh

    Really awesome blog this helped me a great and now im able to send and receive data from server.

    Carry on the good work.

    1. Anupam says:

      Thanks, Ankit. If you want me to cover any topics feel free to comment below!

  22. Nishad Vadgama says:

    how to post json having array of objects like you receiving in response in your example??
    please answer soon..

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages