Youngdev
Youngdev

Reputation: 59

how to handle a JSON array object using retrofit?

I am not so new to Android, but started using retrofit today, I was able to clear all errors, now the response body returns null. I know its something to do with the way my class is set up. I have no idea how to handle an array with arrays. Any help would be appreciated. Thanks

[JSON return[1]

My Interface

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

Class

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;
}

}

Upvotes: 2

Views: 506

Answers (1)

Neo
Neo

Reputation: 3584

Just define a Super class -

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

And use ResponseDS instead of Product class -

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

Hope it will help :)

Upvotes: 1

Related Questions