What I Have
I have a server success response
{
"response_code": 200,
"status": "success",
"message": "enqiry chat fetched successfully",
"meta_data": {
"count": "6"
},
"data": {
"enquiries": [
]
}
}
When Error
, the same API
returns
{
"response_code": 500,
"status": "error",
"meta_data": {
"count": 0
},
"data": [],
"message": "Please specify all required parameter to add enquiries"
}
What happened
At error scenario
the data is changed from JsonObject to JsonArray
My Problem
At success response everything works fine (because I made POJO class
from success response
)
At error response my app crashes saying Tried to read object but found array
What I can't do
I can't change the back-end
, because it is already developed and works for website also .
What I did
I googled and found many solutions which I cannot relate to my issues
POJO
public class ReviewModel {
@SerializedName("data")
private Data mData;
public Data getData() {
return mData;
}
public void setData(Data data) {
mData = data;
}
public class Data {
@SerializedName("reviews")
private List<Review> mReviews;
public List<Review> getReviews() {
return mReviews;
}
public void setReviews(List<Review> reviews) {
mReviews = reviews;
}
}
public class Review {
@SerializedName("comment_date")
private String mCommentDate;
public String getCommentDate() {
return mCommentDate;
}
public void setCommentDate(String comment_date) {
mCommentDate = comment_date;
}
}
}
My solution is to use some thing like this ,
And use a Custom Array Adapter with Gson,
ArrayAdapter class
ArrayAdapterFactory class
And register the adapter factory like this,
This will help to deserialise the json string.