0

I've generated my POJO using http://www.jsonschema2pojo.org/ and now I want to parse my JSONResponse which is of the form:

[  
   {  
    "id":1,
    "name":"xyz",
    "rxg":5,
    "img":"xyz.jpg"
    },
 {  
    "id":2,
    "name":"abc",
    "rxg":9,
    "img":"abc.jpg"
    },
{  },
{  }

And this is where I'm clueless on how to proceed:

    Call<List<MyList>> call = apiInterface.doGetListResources();
    call.enqueue(new Callback<List<MyList>>() {
        @Override
        public void onResponse(Call<List<MyList>> call, 
Response<List<MyList>> response) {
                // How to proceed ahead? I'm completely stuck here

        }

And here's my model class:

public class MyList implements Parcelable {

@SerializedName("id")
@Expose
public int id;
@SerializedName("name")
@Expose
public String name;
@SerializedName("rxg")
@Expose
public int rxg;
@SerializedName("image")
@Expose
public String img;

public final static Parcelable.Creator<MyList> CREATOR = new Creator<MyList>() {

    @SuppressWarnings({
            "unchecked"
    })
    public MyList createFromParcel(Parcel in) {
        MyList instance = new MyList();
        instance.id = ((int) in.readValue((int.class.getClassLoader())));
        instance.name = ((String) in.readValue((String.class.getClassLoader())));
        instance.rxg = ((int) in.readValue((int.class.getClassLoader())));
        instance.img = ((String) in.readValue((String.class.getClassLoader())));
        return instance;
    }

    public MyList[] newArray(int size) {
        return (new MyList[size]);
    }

};

/**
 * No args constructor for use in serialization
 */
public MyList() {
}

/**
 * @param id
 * @param rxg
 * @param name
 * @param img
 */
public MyList(int id, String name, int rxg, String img) {
    super();
    this.id = id;
    this.name = name;
    this.rxg = rxg;
    this.img = img;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getRxg() {
    return rxg;
}

public void setRxg(int rxg) {
    this.rxg = rxg;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(id);
    dest.writeValue(name);
    dest.writeValue(rxg);
    dest.writeValue(img);
}

public int describeContents() {
    return 0;
  }
}

This is for the first time I'm trying Retrofit so please bear with me. And thanks a lot for the help. I've tried to look into similar question here: https://stackoverflow.com/a/39774297/5770629 but couldn't get on further steps.

1 Answer 1

0

First of all, you should check whether the response is successful or not.

If successful then your body is the data you are looking for (provided you have correct POJO class formed)

Else observe error and act accordingly.

Call<List<MyList>> call = apiInterface.doGetListResources();

call.enqueue(new Callback<List<MyList>>() {
    @Override
    public void onResponse(Call<List<MyList>> call,
                                          Response<List<MyList>> response) {
            if(response.isSuccessful(){
                 List< MyList > list = response.body();
            }
            else{
               //throw response.errorbody message to user
            }

    }
Sign up to request clarification or add additional context in comments.

6 Comments

The response is successful but I can't get the data. The reason being that I'm clueless on how to loop here. When using the getters to get the data, I get just nothing.
so u mean as per the code I mentioned if u just log list.size() you get 0, or have you tried logging response.body.string() in successful scenario ?
Now I get this on Logcat: D/OkHttp: ETag: "f6560d9c0785104d09908d052635b92c" And I've tried: if(response.isSuccessful()){ List< MyList > myList = response.body(); Log.v("my_tag", myList.toString()); Log.v("my_tag", "size is : "+list.size()); for (MyList myList : list) { String name = myList.getName(); Log.d("my_tag", "" + " " + name.toString()); } }else{ } Log.d("my_tag", response.code() + ""); }
Also, this one: D/OkHttp: <-- 200 OK
if the log response.body.string prints exact response as you have mentioned in the begining of the question then at least the retrofit call is perfect. Then u need to see which library you are using to parse the response to expected POJO class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.