JSON Parsing in Android using Retrofit

What is Retrofit?

Retrofit is a type-safe HTTP client for Android and Java developed by Square. Using Retrofit we can easily translate the REST APIs to Java Interfaces. Retrofit can be used as the best alternative to Volley. Retrofit Uses OkHttp for making HTTP requests. Retrofit easily translates JSON or XML response to POJO’s (Plain Old Java Objects). In order to Serialize the JSON we can make use of the following Libraries:

Converter Library
Gson com.squareup.retrofit2:converter-gson:2.1.0
Jackson com.squareup.retrofit2:converter-jackson:2.1.0
Moshi com.squareup.retrofit2:converter-moshi:2.1.0
Protobuf com.squareup.retrofit2:converter-protobuf:2.1.0
Wire com.squareup.retrofit2:converter-wire:2.1.0
Simple XML com.squareup.retrofit2:converter-simplexml:2.1.0

We will be using GSON library in this tutorial.

In order to make use of Retrofit, we need 3 classes:

  • Model classes to hold the Response data.
  • An Interface defining possible HTTP Operations and Endpoints.
  • A Retrofit Client class which makes use of the Interface and Retrofit.Builder to specify the URL of the endpoint where HTTP operation is performed.

Creating new Project

Create a new Project named “JSON Parsing Using Retrofit” with package name “com.androiddeft.jsonretrofit“. Create package structure as shown below:


Adding Permissions and Dependencies

Since we will be using GSON for serialization add the GSON dependency along with retrofit and converter-gson dependencies:

Since we are accessing the data from the server add Interner Permission:

Generating POJO’s from JSON

Following is the JSON structure which we will be using in this tutorial:


Now go to jsonschema2pojo.org and copy paste the JSON in order to generate the POJO’s

POJO generation from JSON

Provide the class name as EmployeeList and package as com.androiddeft.jsonretrofit.beans. Choose GSON in Annotation type. Now click on “Preview”. You’ll see 2 classes generated namely Employee.java and EmployeeList.java. Copy paste the 2 classes to com.androiddeft.jsonretrofit.beans package.

Creating API Service

Create an interface named ApiService.java inside com.androiddeft.jsonretrofit.api package. This interface defines possible endpoint URLs and their respective HTTP methods.

Creating Retrofit Client

Now create a class RetroClient.java inside helper package. Here we instantiate Retrofit.Builder using the domain URL.

Displaying data using RecyclerView

Now create a RecyclerView in order to display the parsed data. I have explained in detail on how to create a simple RecyclerView in my previous post. Following are the 2 layout files. 1 containing the RecyclerView and another representing each row in RecyclerView.

We will define the backgroud for each row in the RecyclerView in employee_background.xml inside the drawable folder:

Here goes the adapter for the RecyclerView:

In the MainActivity class, we call the API service using the following code

The Complete MainActivity class:

That’s all. You can see the animation of the app below. You can also download the source code and APK file. In case of any queries or errors, put them in the comments section.

Download Source CodeDownload APK

Retrofit demo animation

Stay Connected

No spam guarantee.



Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.

Leave a Reply

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