Retrofit Android Get JSON Example Step by Step | Get JSON Object

Learn today about Retrofit Android Get JSON Example Step by Step.

We will learn how to parse JSON from URL or Server Using Retrofit Library in android.

You will learn how to fetch/retrieve data from MySQL database in android using json, retrofit and PHP.

When you want to interact with remote server such as to get data from server or to send data to remote server, you need to make http calls.

Retro fit is the library which make this task easier and faster.

Basics about Retrofit

Square have developed the retrofit library.

Retrofit is the REST client for Java and Android development.

This library makes development tasks easier to get or send the data in the JSON format.

Retrofit uses the OkHttp library for HTTP requests. It also handles the cache operations itself with any coding from app developer.

Generally, developers make custom classes to parse JsonArray or JsonObject separately.

In this tutorial, we will get the JSON response in the string format. Then we can use android’s built in classes like JSONArray and JSONObject to parse the JSON in string format.

So now follow all the below steps of this example.

Step 1. Write Retrofit Dependency

First of all, make a new project in android studio.

Write down the following two lines in the build.gradle(Module:app) file.

  • First line will integrate the required classes for retrofit.
  • Second one will get the files for scalar library.

Retrofit will help us to make http calls to the remote web server. While scalar will convert the JSON response in the string format for us.

Give Internet Permission

Add the below line in the AndroidManifest.xml file.

This line will allow our project to use the internet of the android device.

Step 2. Making Interface

For making the http call, we need to create one interface.

Make a new JAVA class and give it a name

Below is the source code for

  • First string variable holds the path of the URL. This variable do not include the name of the file.
  • I have set the file name in the @GET annotation.

Then we need to define the call function. Name of the function is getString().

Step 3. Making the Model class

Model class will help us to maintain the proper data structure.

Make a new class named and copy the below source code in it

  • Our JSON response holds the information like name, country, city, id.
  • So the above class includes the getter and setter methods for all these parameters.

We will use these methods to send and receive data in the file.

Step 4. Final Modifications

When you created the new project in android studio, you should have two file in the project. activity_main.xml and

Write the below code structure in activity_main.xml file

  • I have written two textviews in the above file.
  • First textview is static and it is just saying that below information is fetched using retrofit.
  • Second textview will hold the JSON data. We will insert the data in this textview in Main Activity.

Code snippet for file looks like the below

Reading above source code

In the onCreate() method, compiler will first get the id of the text view.

Then it will call the method getResponse().

getResponse() method looks like the following

  • First of all, compiler will make the object of the Retrofit class.
  • Here, it will bind the URL using .baseUrl() method.
  • Then it will create the object of the interface ApiInterface. Using this object, it will create new Callback and will run the .enqueue() method.
  • After this much, retrofit have make the http call to the URL and it has get the response.
  • Compiler will read the JSON response in the onResponse() method.

Below line will give us the JSON response in the String format.

  • Then compiler will call the writeTv(jsonresponse) method. Compiler will pass the string variable which holds the JSON response in the parameter of this method.

Code structure for writeTv(jsonresponse) method is as the below

For understanding above method, let us first check the JSON data which we have got.

  • First of all, compiler will parse the parent object from the JSON response.
  • Then from this parent object, it will check the value of the “status” field.
  • If it is true, then it will create one arraylist with objects of the RetroModel class, which we have written in the Step 3.
  • Now compiler will create one JSONArray which is represented by the field “data“.
  • After this, it will make one for loop. During every iteration of this for loop, compiler will create the object of the RetroModel class and it will set the data with this object.
  • Then it will add this object into the arraylist.

Now another for loop is there. In this for loop, compiler will set the data in to the text view.

Check Final Texts

Below video demonstrate how the outcome of this project will look a like

Download Source Code For retrofit android get json example

Click me Download Json_Parsing_URL_Retrofit Source Code

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

3 Responses

  1. Rafay Maqsood says:

    Can you please tell me how to save this JSON file using php and mysql, then access it through my Android App?

  2. Eswar says:

    Hi, I used your code for one of my API call, its responding after 5 seconds, meanwhile other services getting loaded. So its not checking the condition properly.

    can you please tell how to avoid this 5 seconds delay in response.

Leave a Reply

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