我不是 Android 新手,但今天开始使用 retrofit,我能够清除所有错误,现在响应主体返回 null。我知道这与我的类(class)设置方式有关。我不知道如何处理带有数组的数组。任何帮助,将不胜感激。谢谢
[
我的界面
@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;
}
最佳答案
只需定义一个父类(super class) -
public class ResponseDS{
public boolean success;
public Product[] products;
}
并使用 ResponseDS 而不是 Product 类 -
@GET("/web-api.php?route=feed/web_api/products")
Call<ResponseDS> loadProducts(@Query("category") Integer id, @Query("key") String apiKey);
希望对你有帮助:)
关于android - 如何使用改造处理 JSON 数组对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38125388/