如何使用Retrofit解析JSONArray

时间:2017-08-11 20:34:25

标签: android retrofit2 gson

我使用http://www.jsonschema2pojo.org/生成了我的POJO,现在我要解析我的JSONResponse格式:

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

这就是我对如何继续进行无能为力的事情:

    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

        }

这是我的模特课:

public class MyList实现了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;
  }
}

这是我第一次尝试改装,所以请耐心等待。非常感谢你的帮助。我试图在这里查看类似的问题:https://stackoverflow.com/a/39774297/5770629但无法继续采取进一步措施。

1 个答案:

答案 0 :(得分:0)

首先,您应该检查响应是否成功。

如果成功,那么您的身体就是您正在寻找的数据(假设您已经形成了正确的POJO课程)

否则会发现错误并采取相应行动。

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
            }

    }