android retrofit example json

Get JSON Array using Retrofit Android Example

I used volley and Retrofit on my project as per project requirement. In this retrofit android example article I am going to show you how to get JSON object or JSON Array from URL using Retrofit library. So first understand the benefit of both the Libraries to make HTTP calls.

Volley Library is build by Google and Retrofit is build be Square. Retrofit is definitely the better alternative to volley in terms of ease of use, performance, flexibility and other things. Using this tool android developer can make all network stuff much more easier.

Use Android Retrofit if your project is based on a standard REST API with JSON responses. Use Volley is really good in terms of Caching and Retries.

One important point that you have to keep remember if you want to download large files or streaming then don’t use any of Retrofit or Volley. Go for Download Manager because large files require buffers and resume features.

Watch Video Retrofit Android Example

Before start coding please look at the JSON object File and JSON Array File that I will use during this project. You can create your own JSON file ar per need. Here is my JSON Object and JSON Array File. You have to add Retrofit library dependency on app gradle file. There you have to use JSON converter library to get actual value from json file.

// retrofit, gson
    compile ''
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Step by Step Guide

1) Add Dependency on Gradle file
2) Understand Retrofit structure
3) Create Model(POJO) class
4) Create Interface (Service) for Retrofit
5) Create Retrofit CallBack Request from Main Activity
6) Run App in Real Device using USB Debugging


Login & Download source code


50 days android course


Add Dependency on Gradle file

Create New Project in android studio. My project name is Retrofit_json. Now open app module gradle file and add following dependency in dependency section.

   compile ''
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Add Internet Permission in Android Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

    <uses-permission android:name="android.permission.INTERNET"/>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Understand Retrofit Structure

Retrofit Library is work on three steps. First is Model class that is POJO model. You have to create all the variable name that are present on your JSON file. For example in my JSON file there are string name as “rom” and “screenSize”, so I created string type variable with same name in my model class. Remember String name must be same otherwise it will not work.

Second is Interface or Service that we have to call from main activity. You have to define your sub-url from where you want json data, Callback return type and the method name in this Interface file.

android retrofit 2 json

Third is Retrofit request queue. You have to make request from make activity by initializing retrofit and interface. The response will be return on the format that you will define in return type in retrofit interface.

Create Model(POJO) class

Create new Java class and name it as Create all the string, integer and object variable that present on your json file. below is my class

package blueappsoftware.retrofit_json;

 * Created by Jaink on 24-08-2017.

public class model {

    private String rom;
    private String screenSize;
    private String backCamera;

    // getter / setter

    public String getRom(){return  rom; }
    public void setRom(String mrom){ this.rom = mrom;}

    public String getScreenSize(){ return  screenSize; }
    public void setScreenSize(String mscreensize){ this.screenSize = mscreensize; }

    public String getBackCamera(){ return  backCamera;}
    public void setBackCamera(String mBackcamera){ this.backCamera = mBackcamera; }

Create Interface (Service) for Retrofit

You can create multiple interface as per your need. I am creating two interface- one for JSON Object and one for JSON Array. In this Interface you have to write four point. 1) GET/POST method type 2) Sub-url string from where you want to fetch json data 3) method name- give it any name. 4) Callback return type – it will be model type of list of model type.

If your JSON file in object type than interface callback return type will Model only. If you JSON file in Array type than the return type will be list.

package blueappsoftware.retrofit_json;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;

 * Created by Jaink on 24-08-2017.

public interface RetrofitArrayAPI {

    Call<List<model>> getJsonArrayData();

package blueappsoftware.retrofit_json;

import retrofit2.Call;
import retrofit2.http.GET;

 * Created by Jaink on 24-08-2017.

public interface RetrofitObjectAPI {

    Call<model> getJsonObjectData();

Create Retrofit CallBack Request from Main Activity

Now the final thing is to create retrofit variable and call the Interface methods. To initialize retrofit variable, you have to pass BaseURL means your website primary domain name and JSON converter. Now initialize interface and call the method inside interface. The final step is call.enqueue request that will return response in Model type on OnResponse method. Below is complete code of file

package blueappsoftware.retrofit_json;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    TextView txt1, txt2, txt3;
    String url = "";
    protected void onCreate(Bundle savedInstanceState) {

        txt1 =(TextView) findViewById(;
        txt2 =(TextView) findViewById(;
        txt3 =(TextView) findViewById(;



    public void getRetrofitObject(){
        Retrofit retrofit = new Retrofit.Builder()

        RetrofitObjectAPI service = retrofit.create(RetrofitObjectAPI.class);
        Call<model> call = service.getJsonObjectData();
        call.enqueue(new Callback<model>() {
            public void onResponse(Call<model> call, Response<model> response) {
                Log.e(" mainAction", "  response "+ response.body().toString());
                Log.e(" mainAction", "  rom - "+ response.body().getRom().toString());
                Log.e(" mainAction", "  rom - "+ response.body().getScreenSize().toString());
                Log.e(" mainAction", "  rom - "+ response.body().getBackCamera().toString());

            public void onFailure(Call<model> call, Throwable t) {
                Log.e("MainActivity ", "  error "+ t.toString());


    public void getRetrofitArray(){



Run the Code in Real Device using USB Debugging

Now you have to run this code. You will see json reponse in logcat. I have placed one Textview on activity.xml file to display the response on screen. I hope after learn from this Retrofit android example article, you can easily get json array, image and string from URL using Retrofit library.

Please leave your comment about video lesson and what more you want to learn from me.

Read MoreCreate Facebook Messenger type Floating Icon and Send FCM Notification to Device

50 days android course





Posted in andorid.

I Am Post Graduation (M.Tech. C.S.E) from VNIT, Nagpur. I Have Published Two Research Paper On International Journal. My Apps on Google Play Store- Sanyukt Parivar(Mp Police ), NOTOtag- Digital Chhindwara, The Tubby-VoiceRecognizer & More.
Contact me at


  1. I love your blog.. very nice colors & theme. Did you create this
    website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to create my own blog and would like to find
    out where u got this from. appreciate it

  2. Pretty great post. I just stumbled upon your weblog and wanted to say
    that I’ve truly loved browsing your blog posts.
    In any case I’ll be subscribing for your feed and I hope you write once more very soon!

  3. Hi there! This post could not be written any better!
    Reading through this post reminds me of my good old room mate!
    He always kept talking about this. I will forward this article to him.
    Fairly certain he will have a good read. Thank you for sharing!

  4. Hi there! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly?
    My weblog looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might
    be able to fix this issue. If you have any recommendations, please share.
    Thank you!

  5. This is really attention-grabbing, You are an overly skilled blogger.
    I have joined your feed and look forward to in the hunt
    for extra of your great post. Also, I have shared your site in my social networks

  6. I’m really enjoying the theme/design of your blog.

    Do you ever run into any browser compatibility issues?

    A few of my blog audience have complained about my site not
    working correctly in Explorer but looks great in Chrome.
    Do you have any solutions to help fix this issue?

  7. I was recommended this web site by my cousin. I’m
    not sure whether this post is written by him as no one else
    know such detailed about my problem. You are wonderful!


  8. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

  9. Having read this I thought it was very informative. I appreciate you finding the time and
    energy to put this short article together. I once again find myself personally spending a lot of time both reading and posting comments.
    But so what, it was still worthwhile!

  10. hi! may i know is your code complete? because i run the program and does nothing, i have download the given source code.

    • Yes this code is working…You have to create file on php server to store FCM key into your database for future use. First check on your android studio log whether FCM token is printing OR not. If it is printing in log than send token to your web server.

  11. Good day! I could have sworn I’ve been to
    this website before but after going through
    a few of the articles I realized it’s new to me.

    Nonetheless, I’m certainly delighted I found it and I’ll be book-marking it and checking back regularly!

  12. Spot on with this write-up, I absolutely feel this website needs much more attention. I’ll probably be returning to read
    through more, thanks for the info!

  13. Hey very cool website!! Man .. Excellent ..
    Amazing .. I will bookmark your site and take the feeds additionally?
    I am glad to search out numerous useful info here
    in the submit, we need work out more strategies
    on this regard, thanks for sharing. . . . . .

  14. Pingback: ExoPlayer Android Example- Best Alternative of VideoView By Kamal

  15. Pingback: Simplest way to get WordPress Post in Android App- REST API

  16. I just like the helpful information you supply to your articles.

    I’ll bookmark your blog and test once more here frequently.
    I’m fairly sure I will learn many new stuff proper here!
    Good luck for the following!

Comments are closed.