Samaludheen
Samaludheen

Reputation: 176

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

Could not parse the response which have array inside object.

My response data :

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

My 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 + '\'' +
        "}";
    }
}

Data class for the array in response : 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 + '\'' +
                    "}";
}

My call to the 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);

The response body I get is SearchSuppliers{status = 'false',message = 'null',data = 'null',code = '0'} , where it should be the sample I mentioned above. I get the desired response if I change the call as JsonObject:

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

Upvotes: 0

Views: 780

Answers (2)

Intsab Haider
Intsab Haider

Reputation: 3561

One option is Change variable names to uppercase like

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

and similarly in array type class

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

as per json keys..

Upvotes: 1

Abdul Kawee
Abdul Kawee

Reputation: 2727

You can do it as

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

Hope this helps you.

Upvotes: 2

Related Questions