Skip to content Skip to sidebar Skip to footer

Retrofit --> Java.lang.illegalstateexception: Expected Begin_array But Was Begin_object At Line 1 Column 2 Path $

None of the posts i found have helped me understand what im supposed to do. Stuck on this for quite a while so asking here. I want the List of IN and OUT names as 'in' and 'out' ar

Solution 1:

Right way to parse your server response

Change your interface

@GET("/")
Call<Example> getInUsers();

Change call

Call<Example> call1 = api.getInUsers();
    call1.enqueue(new Callback<Example>() {
        @Overridepublic void onResponse(Call<Example> call1, Response<Example> response) {
            List<In> inUsers = response.body().getPlanData().getVotesData().getVotes().getIn();
            ...your other  code here...

Solution 2:

you have to do like below.

publicinterfaceAPIInterface {
@GET("/")
Call<Example> getInUsers();

    @GET("/")
    Call<List<Out>> getOutUsers();
}

Your activity like below.

Call<Example> call1 = api.getInUsers();
    call1.enqueue(newCallback<Example>() {
        @OverridepublicvoidonResponse(Call<Example> call1, Response<Example> response) {
            List<In> inUsers = response. getPlanData(). getVotesData().getVotes().getIn();
            Log.v("InUsers",String.valueOf(inUsers));
            data.add(newVote(Vote.HEADER_TYPE,"IN"));
            mAdapter.notifyDataSetChanged();
            for(In vote : inUsers) {
                data.add(newVote(Vote.ITEM_TYPE,String.valueOf(vote)));
            }


        }

        @OverridepublicvoidonFailure(Call<Example> call1, Throwable t) {
            Toast.makeText(getActivity().getApplicationContext(), t.getMessage() + "IN LIST", Toast.LENGTH_LONG).show();
        }
    });

Solution 3:

You are trying to get Json Array from response but the rsponse contains JsonObject

Solution 4:

The API doesn't return a List<In> or List<Out>. Instead, it returns an Example object. So instead of

public interface APIInterface {
@GET("/")
Call<List<In>> getInUsers();

    @GET("/")
    Call<List<Out>> getOutUsers();
}

you should use

publicinterfaceAPIInterface {
    @GET("/")
    Call<Example> getUsers();
}

So your call should look like this:

Call<Example> call = api.getInUsers();
call.enqueue(new Callback<Example>() {
    @Overridepublic void onResponse(Call<Example> call1, Response<Example> response) {
        // ... whatever you want to do with that data.// E.g. you can access inUsers via response.body().getPlanData().getVotesData().getVotes().getIn();
    }

    // ...
});

Post a Comment for "Retrofit --> Java.lang.illegalstateexception: Expected Begin_array But Was Begin_object At Line 1 Column 2 Path $"