This question already has an answer here:
- Gson, how to deserialize array or empty string 6 answers
I have a problem.
I'm using Retrofit 2.0
to make calls to my api from my Android app. All works fine, but when I'm receive a empty field I get this error:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 8599 path $.meta.pagination.links
The problem is with links field, that sometimes is not empty
"links": {
"next": "http://www.example.com,
}
But when it's empty the error appears. My questions, How can i handle when the links field is empty?
This is my whole response:
{
"data": [
{
.....
}
],
"meta": {
"pagination": {
"total": 50,
"count": 50,
"per_page": 60,
"current_page": 1,
"total_pages": 1,
"links": []
}
}
}
And this my POJO class:
public class ListResponse<O> {
@SerializedName("data")
private List<O> lista;
@SerializedName("meta")
private Meta meta;
public List<O> getLista() {
return lista;
}
public String getNext() { return meta.getPagination().getLinks().getNext(); }
public int getTotal() { return meta.getPagination().getTotal(); }
public class Meta {
@SerializedName("pagination")
Pagination pagination;
public Pagination getPagination(){ return pagination; }
public class Pagination{
@SerializedName("total")
int total;
@SerializedName("count")
int count;
@SerializedName("per_page")
int per_page;
@SerializedName("current_page")
int current_page;
@SerializedName("total_pages")
int total_pages;
@SerializedName("links")
Links links;
public int getTotal() {
return total;
}
public Links getLinks() {
return links;
}
public class Links {
@SerializedName("next")
String next;
public String getNext() {
return next;
}
}
}
}