Tayyab Husain
Tayyab Husain

Reputation: 51

How to get individual json object from an api response?

Actually, I am Working on a Quran API where the API response is

{
 "data": 
  {
    "surahs"[...],
    "edition":{...}
  }
}

In this response, I just want surah Array, which looks like

"surahs": [
        {
            "number": 1,
            "name": "سُورَةُ ٱلْفَاتِحَةِ",
            "englishName": "Al-Faatiha",
            "englishNameTranslation": "The Opening",
            "revelationType": "Meccan",
            "ayahs"[
               {
                    "number": 1,
                    "audio": "Some Url",
                    "text": "بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ",
                    "numberInSurah": 1,
                    "juz": 1,
                    "manzil": 1,
                    "page": 1,
                    "ruku": 1,
                    "hizbQuarter": 1,
                    "sajda": false
                },
                {...}
                {...}
            ]
        }
        {...}
        {...}
      ]

This is what I want from this response but I am confused about how to get it, I am using retrofit 2 for making HTTP requests

these are my POJO classes

public class Surah
{
    private int number;
    private String name;
    private String englishName;
    private String englishNameTranslation;
    private String revelationType;
    private List<Ayah> ayahs;
}

public class Ayah 
{
    private int number;
    private String audio;
    private String text;        //this field heave multiple languages
    private int numberInSurah;
    private int juz;
    private int manzil;
    private int page;
    private int ruku;
    private int hizbQuarter;
    private Boolean sajda;
}

And this is the API call

public void MyApi(){
        WebApi mWebApi = WebApi.retrofit.create(WebApi.class);
        Call<Surah> call = mWebApi.getAllSurah();
        call.enqueue(new Callback<Surah>() {
            @Override
            public void onResponse(Call<Surah> call, Response<Surah> response) {
                if (response.isSuccessful()){

                }
            }
            @Override
            public void onFailure(Call<Surah> call, Throwable t) {
                Log.d(TAG, "onFailure:"+t.toString());
                Toast.makeText(MainActivity.this, "Response is OnFailure", Toast.LENGTH_SHORT).show();
            }
        });
    }

however, i intentionally changed the getAllSurah Type List to single object because then I was getting an exception like "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $", because it was returning the whole json object as a response.

Upvotes: 0

Views: 998

Answers (1)

Priyanka
Priyanka

Reputation: 1875

You need to add one more class to hold the whole response that you are getting from API

public class Data {

@SerializedName("surahs")
@Expose
private List<Surah> surahs = null;
@SerializedName("edition")
@Expose
private Edition edition;

public List<Surah> getSurahs() {
return surahs;
}

public void setSurahs(List<Surah> surahs) {
 this.surahs = surahs;
}

public Edition getEdition() {
 return edition;
}

public void setEdition(Edition edition) {
this.edition = edition;
}

}

Then make changes in your api call as below

public void MyApi(){
    WebApi mWebApi = WebApi.retrofit.create(WebApi.class);
    Call<Data> call = mWebApi.getAllSurah();
    call.enqueue(new Callback<Data>() {
        @Override
        public void onResponse(Call<Data> call, Response<Data> response) {
            if (response.isSuccessful()){
                response.getSurahs()  // this will return surah array to you
            }
        }
        @Override
        public void onFailure(Call<Data> call, Throwable t) {
            Log.d(TAG, "onFailure:"+t.toString());
            Toast.makeText(MainActivity.this, "Response is OnFailure", 
            Toast.LENGTH_SHORT).show();
        }
    });
}

Upvotes: 2

Related Questions