Tlaloc-ES
Tlaloc-ES

Reputation: 5282

Expected BEGIN_ARRAY but was BEGIN_OBJECT error with gson, android, retrofit

Hi I am trying to get data from the API and I am getting the next error:

Expected BEGIN_ARRAY but was BEGIN_OBJECT

The data that I am getting is the following:

 "code": "01",
    "data": {
        "code": "0",
        "time": "14-05-2019 16:22:28.661",
        "data": [
            {
                "geometry": {
                    "BoundingBox": null,
                    "ExtraMembers": null,
                    "Type": 7,
                    "Coordinates": {
                        "Latitude": 40.401609,
                        "Values": [
                            -3.674735,
                            40.401609
                        ],
                        "Longitude": -3.674735
                    },
                    "CoordinateReferenceSystem": null
                }
            },
            {
                "geometry": {
                    "BoundingBox": null,
                    ....

And the pojo for get this data is the next:

public class ApiResponse<T extends ApiResponseData> {
    ...   
    @SerializedName("data")
    @Expose
    private List<T> data;
    ...
}

public class DData implements ApiResponseData {
...

And the retrofit call for get data is the next:

public void getListOfDs(final MutableLiveData<ApiResponse<DData>> data) {
        Call<ApiResponse<DData>> call = CApiInterface.getListOfDs(ID_CLIENT);
        call.enqueue(new Callback<ApiResponse<DData>>() {
            @Override
            public void onResponse(Call<ApiResponse<DData>> call, Response<ApiResponse<DData>> response) {
                ApiResponse<DData> apiResponse = response.body();
                data.setValue(apiResponse);
            }

            @Override
            public void onFailure(Call<ApiResponse<DData>> call, Throwable t) {
                data.setValue(createFailedResponse());
                call.cancel();
            }
        });
    }

The question is why am I getting this error, in the ApiResponse I have a List in this case List and I am getting a list of DData, for another case like the response is only a single DData this work perfectly any idea?

Any idea that how can solve it?

Thanks.

Upvotes: 0

Views: 88

Answers (2)

TheAnkush
TheAnkush

Reputation: 915

"Data" key is twice, one in top is of object type but it should be of type list/Array. Your response should with in brackets[] currently {} represents Json Object, not list.

Refer this to understand difference between JSONArray and JSONObject Difference between JSONObject and JSONArray

Upvotes: 1

Romain Goutte-Fangeas
Romain Goutte-Fangeas

Reputation: 789

You're trying to deserialize the data object as a List but the first "data" in the hierarchy is an object, what causes the exception. You need to review your Pojo class to fit the Json structure.

Maybe something like this can work:

public class ApiResponse<T extends ApiResponseData> {
    ...
    @SerializedName("data")
    @Expose
    private Data<T> data;
    ...
}

public class Data<T extends ApiResponseData>  {

    @SerializedName("code")
    @Expose
    private String code;

    @SerializedName("time")
    @Expose
    private String time;

    @SerializedName("data")
    @Expose
    private List<T> data;

    ...
}

Upvotes: 1

Related Questions