0

我必须使用一个返回给我的后端:

{
    "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
4

1 回答 1

0

我只是解决这个问题。

我替换private WrapperData <T> wrapperData;private <T> wrapperDate;所以当我想获取我调用的列表时Call<GeneralResponse<Collection<PermissionTypes>>>

于 2020-05-28T15:56:08.387 回答