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

Error receiving response using retrofit 2. Could not parse array in object

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

我的回复数据:

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

我的 POJO classes : 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 + '\'' +
        "}";
    }
}

响应数组的数据class: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);

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

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

数组类型类似class

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

根据json键..

你可以这样做

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

希望对您有所帮助。