Retrofit --> java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

假如想象 提交于 2019-12-13 03:39:35

问题


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" are json arrays. The error shows up as exception in OnFailure of response. getUsers() is in main activity.

private void getUsers() {


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    APIInterface api = retrofit.create(APIInterface.class);

    Call<List<In>> call1 = api.getInUsers();
    call1.enqueue(new Callback<List<In>>() {
        @Override
        public void onResponse(Call<List<In>> call1, Response<List<In>> response) {
            List<In> inUsers = response.body();
            Log.v("InUsers",String.valueOf(inUsers));
            data.add(new Vote(Vote.HEADER_TYPE,"IN"));
            mAdapter.notifyDataSetChanged();
            for(In vote : inUsers) {
                data.add(new Vote(Vote.ITEM_TYPE,String.valueOf(vote)));
            }


        }

        @Override
        public void onFailure(Call<List<In>> call1, Throwable t) {
            Toast.makeText(getActivity().getApplicationContext(), t.getMessage() + "IN LIST", Toast.LENGTH_LONG).show();
        }
    });
}

json is :

 {
 "message": "Hello World.",
 "planData": {
     "planId":"44444410",
     "votesData": {
         "votes": {
             "in":[ 
                 {
                     "firstName" : "Raj",
                     "lastName" : "Taj"
                 },
                 {
                     "firstName" : "Badger",
                     "lastName" : "Tagger"
                 },
                  {
                     "firstName" : "Candle",
                     "lastName" : "Bangle"
                 }
                 ] , 
                 "out" : [
                     {
                     "firstName" : "Bonnie",
                     "lastName" : "Clooney"
                 },
                 {
                     "firstName" : "Clyde",
                     "lastName" : "Hyde"
                 }
                 ]
         }
     }
 }
}

The Interface:

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

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

I got the models from http://www.jsonschema2pojo.org/

in example class there was only message and planId. I was calling the example class in interface initially. but thought the error might be because of that so changed it to List but nothing.

POJO code from the site:

>  
> -----------------------------------com.example.Example.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class Example {
> 
> @SerializedName("message") @Expose private String message;
> @SerializedName("planData") @Expose private PlanData planData;
> 
> public String getMessage() { return message; }
> 
> public void setMessage(String message) { this.message = message; }
> 
> public PlanData getPlanData() { return planData; }
> 
> public void setPlanData(PlanData planData) { this.planData = planData;
> }
> 
> }
> -----------------------------------com.example.In.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class In {
> 
> @SerializedName("firstName") @Expose private String firstName;
> @SerializedName("lastName") @Expose private String lastName;
> 
> public String getFirstName() { return firstName; }
> 
> public void setFirstName(String firstName) { this.firstName =
> firstName; }
> 
> public String getLastName() { return lastName; }
> 
> public void setLastName(String lastName) { this.lastName = lastName; }
> 
> }
> -----------------------------------com.example.Out.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class Out {
> 
> @SerializedName("firstName") @Expose private String firstName;
> @SerializedName("lastName") @Expose private String lastName;
> 
> public String getFirstName() { return firstName; }
> 
> public void setFirstName(String firstName) { this.firstName =
> firstName; }
> 
> public String getLastName() { return lastName; }
> 
> public void setLastName(String lastName) { this.lastName = lastName; }
> 
> }
> -----------------------------------com.example.PlanData.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class PlanData {
> 
> @SerializedName("planId") @Expose private String planId;
> @SerializedName("votesData") @Expose private VotesData votesData;
> 
> public String getPlanId() { return planId; }
> 
> public void setPlanId(String planId) { this.planId = planId; }
> 
> public VotesData getVotesData() { return votesData; }
> 
> public void setVotesData(VotesData votesData) { this.votesData =
> votesData; }
> 
> }
> -----------------------------------com.example.Votes.java-----------------------------------
> 
> package com.example;
> 
> import java.util.List; import com.google.gson.annotations.Expose;
> import com.google.gson.annotations.SerializedName;
> 
> public class Votes {
> 
> @SerializedName("in") @Expose private List<In> in = null;
> @SerializedName("out") @Expose private List<Out> out = null;
> 
> public List<In> getIn() { return in; }
> 
> public void setIn(List<In> in) { this.in = in; }
> 
> public List<Out> getOut() { return out; }
> 
> public void setOut(List<Out> out) { this.out = out; }
> 
> }
> -----------------------------------com.example.VotesData.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class VotesData {
> 
> @SerializedName("votes") @Expose private Votes votes;
> 
> public Votes getVotes() { return votes; }
> 
> public void setVotes(Votes votes) { this.votes = votes; }
> 
> }

回答1:


you have to do like below.

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

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

Your activity like below.

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


        }

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



回答2:


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>() {
        @Override
        public void onResponse(Call<Example> call1, Response<Example> response) {
            List<In> inUsers = response.body().getPlanData().getVotesData().getVotes().getIn();
            ...your other  code here...



回答3:


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




回答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

public interface APIInterface {
    @GET("/")
    Call<Example> getUsers();
}

So your call should look like this:

Call<Example> call = api.getInUsers();
call.enqueue(new Callback<Example>() {
    @Override
    public 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();
    }

    // ...
});


来源:https://stackoverflow.com/questions/46581681/retrofit-java-lang-illegalstateexception-expected-begin-array-but-was-begin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!