我必须使用一个返回给我的后端:
{
"ok": true,
"data": [
{
"id": 0,
"name": "Intempestivo"
},
{
"id": 1,
"name": "Vacaciones"
}
}
但其他时候它返回给我的是这样的:
{
"ok": true,
"data": [
{
"id": 10,
"startDate": "2020-05-29T12:00:00.000Z",
"endDate": "2020-05-30T12:00:00.000Z",
"status": "En revision",
"type": "Vacaciones"
},
{
"id": 11,
"startDate": "2020-05-29T12:00:00.000Z",
"endDate": "2020-05-30T12:00:00.000Z",
"status": "En revision",
"type": "Vacaciones"
}
]
}
我如何将这些响应包装在一个通用响应类中,其中唯一改变的可能是数据 POJO 对象。
public class GeneralResponse<T> {
@SerializedName("data")
private WrapperData<T> wrapperData;
@SerializedName("ok")
private boolean ok;
@SerializedName("error")
private Error error;
}
包装数据是:
public class WrapperData<T> {
private T dataResponse;
public WrapperData(T POJOResponse) {
this.dataResponse = POJOResponse;
}
public T getDataResponse() {
return dataResponse;
}
}
改造接口有一个这样的方法:
@GET("permission_types/")
Call<GeneralResponse<ArrayList<PermissionType>>> getPermissionTypesWrapped();
但是当我提出请求时,我得到了以下异常:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 'n' column 'n' path $.data