У меня есть 2 типа ответа для одного URL. Первый — это список объектов, а второй — объект ответа на ошибку.
Первый:
[
{
id: 1,
title: "title1",
description: "",
position: 1000
},
{
id: 2,
title: "title3",
description: "",
position: 1000
},
{
id: 3,
title: "title3",
description: "",
position: 1000
}
]
Второй:
{
"status":"error",
"error":"no token"
}
Любые идеи, как справиться с этим с помощью Retrofit 2? Когда сервер возвращает ответ об ошибке, у меня возникает ошибка: «Ожидается BEGIN_ARRAY, но был BEGIN_OBJECT в строке 1, столбце 2, пути $»
Категория.класс
public class Category{
@SerializedName("id")
@Expose
private Id id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("position")
@Expose
private Integer position;
/**
*
* @return
* The id
*/
public Id getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Id id) {
this.id = id;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The description
*/
public String getDescription() {
return description;
}
/**
*
* @param description
* The description
*/
public void setDescription(String description) {
this.description = description;
}
/**
*
* @return
* The position
*/
public Integer getPosition() {
return position;
}
/**
*
* @param position
* The position
*/
public void setPosition(Integer position) {
this.position = position;
}
}
Запрос к серверу:
public void loadCategories(final boolean pullToRefresh) {
Call<List<Category>> categoriesCall = mRequestHandler.getCategories(Keys.CATEGORY_CATALOGS);
categoriesCall.enqueue(new Callback<List<Category>>() {
@Override
public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
if (response.isSuccess()) {
getView().setData(response.body());
}
}
@Override
public void onFailure(Call<List<Category>> call, Throwable t) {
getView().showError(null, pullToRefresh);
Log.e("Error:", t.getMessage());
}
});
}