使用改造2收到响应时出错。无法解析对象中的数组

时间:2018-09-18 10:09:56

标签: android json retrofit2 response

无法解析对象内部具有数组的响应。

我的回复数据:

{
    "Status":true,
    "Code":0,
    "Message":"Data read successfully...",
    "Data":[
        {
            "PSID":"w",
            "Country":"w",
            "City":"w",
            "NumberOfLikes":0
        }   
    ]
}

我的POJO类:SearchSuppliers.java

public class SearchSuppliers{
private boolean status;
private String message;
private List<DataItemSearchSuppliers> data;
private int code;

public void setStatus(boolean status){
    this.status = status;
}

public boolean isStatus(){
    return status;
}

public void setMessage(String message){
    this.message = message;
}

public String getMessage(){
    return message;
}

public void setData(List<DataItemSearchSuppliers> data){
    this.data = data;
}

public List<DataItemSearchSuppliers> getData(){
    return data;
}

public void setCode(int code){
    this.code = code;
}

public int getCode(){
    return code;
}

@Override
public String toString(){
    return 
        "SearchSuppliers{" + 
        "status = '" + status + '\'' +
        ",message = '" + message + '\'' +
        ",data = '" + data + '\'' +
        ",code = '" + code + '\'' +
        "}";
    }
}

作为响应的数组的数据类:DataItemSearchSuppliers.java

public class DataItemSearchSuppliers {

private String pSID;
private String country;
private String city;
private int numberOfLikes;

public void setPSID(String pSID){
    this.pSID = pSID;
}

public String getPSID(){
    return pSID;
}
public void setCountry(String country){
    this.country = country;
}
public String getCountry(){
    return country;
}
    public void setCity(String city){
    this.city = city;
}

public String getCity(){
    return city;
}
public void setNumberOfLikes(int numberOfLikes){
    this.numberOfLikes = numberOfLikes;
}

public int getNumberOfLikes(){
    return numberOfLikes;
}

@Override
public String toString(){
    return
            "DataItem{" +
                    "pSID = '" + pSID + '\'' +
                    ",country = '" + country + '\'' +
                    ",city = '" + city + '\'' +
                    ",numberOfLikes = '" + numberOfLikes + '\'' +
                    "}";
}

我对api的调用:

@Headers("Content-Type: application/json; charset=utf-8")
@POST("api/supplier/listSupplier")
Call<SearchSuppliers> searchSuppliersCall(@Query("searchcode") int searchCode, @Query("start") int start, @Query("end") int end,@Body JsonObject body);

我得到的响应正文为 SearchSuppliers {status ='false',message ='null',data ='null',code ='0'} ,应该是我上面提到的示例。如果将调用更改为JsonObject,则会得到所需的响应:

Call<JsonObject> searchSuppliersCall(@Query("searchcode") int searchCode, @Query("start") int start, @Query("end") int end,@Body JsonObject body);

2 个答案:

答案 0 :(得分:2)

您可以按照以下方式操作

@SerializedName("Status")
@Expose
private boolean status;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("Data")
@Expose
private List<DataItemSearchSuppliers> data;
@SerializedName("Code")
@Expose
private int code;

希望这对您有所帮助。

答案 1 :(得分:1)

一个选项是将变量名更改为大写,例如

private boolean Status;
private String Message;
private List<DataItemSearchSuppliers> Data;
private int Code;

以及类似的数组类型类

private String PSID;
private String Country;
private String City;
private int NumberOfLikes;

根据json键。