如何使用改造来处理JSON数组对象?

时间:2016-06-30 13:59:20

标签: android json api retrofit

我对Android不是很新,但今天开始使用改造,我能够清除所有错误,现在响应体返回null。我知道它与我的课程设置方式有关。我不知道如何使用数组处理数组。任何帮助,将不胜感激。感谢

[JSON return[1]

我的界面

  @GET("/web-api.php?route=feed/web_api/products")
 Call<Product>  loadProducts(@Query("category") Integer id, @Query("key")         String apiKey);

public class Product implements Serializable {
@SerializedName("id")
private long mId;
@SerializedName("name")
private String mname;
@SerializedName("description")
private String mText;
@SerializedName("price")
private Double mprice;
@SerializedName("href")
private String mproductURL;
@SerializedName("thumb")
private String mImageURL;

public Product(long mId, String mname, String mText, Double mprice, String mproductURL, String mImageURL) {
    this.mId = mId;
    this.mname = mname;
    this.mText = mText;
    this.mprice = mprice;
    this.mproductURL = mproductURL;
    this.mImageURL = mImageURL;
}

public long getmId() {
    return mId;
}

public void setmId(long mId) {
    this.mId = mId;
}

public String getMname() {
    return mname;
}

public void setMname(String mname) {
    this.mname = mname;
}

public String getmText() {
    return mText;
}

public void setmText(String mText) {
    this.mText = mText;
}

public Double getMprice() {
    return mprice;
}

public void setMprice(Double mprice) {
    this.mprice = mprice;
}

public String getMproductURL() {
    return mproductURL;
}

public void setMproductURL(String mproductURL) {
    this.mproductURL = mproductURL;
}

public String getmImageURL() {
    return mImageURL;
}

public void setmImageURL(String mImageURL) {
    this.mImageURL = mImageURL;
}


@Override
public String toString() {
    return mText;
}

}

1 个答案:

答案 0 :(得分:1)

只需定义一个超类 -

public class ResponseDS{
    public boolean success;
    public Product[] products;
}

并使用ResponseDS而不是Product class -

 @GET("/web-api.php?route=feed/web_api/products")
 Call<ResponseDS>  loadProducts(@Query("category") Integer id, @Query("key")         String apiKey);

希望它会有所帮助:)