简体   繁体   中英

Expected BEGIN_ARRAY but was BEGIN_OBJECT retrofit2

I have some problem about retrofit2

How to fix Expected BEGIN_ARRAY but was BEGIN_OBJECT in retrofit2

I don't know to fix it

What's my wrong ?

my json is

{
    "result": true,
    "message": "success",
    "data": [
        {
            "id": "10",
            "type_name": "pppppp"
        },
        {
            "id_mt": "11",
            "type_name": "aaaaaa"
        }
    ]
}

and my model class is

public class MessageFastModel {
    private boolean result;
    private String message;

    private List<DataBean> data;

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        private String id;
        private String type_name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getType_name() {
            return type_name;
        }

        public void setType_name(String type_name) {
            this.type_name = type_name;
        }
    }
}

My code is

 @FormUrlEncoded
 @POST("api/message_type")
 Call<MessageFastModel> listMessageFast(@Field("token") String token);

and

Call<MessageFastModel> dddddCall = ooooo.listMessageFast(Preferences.getToken());
                        dddddCall.enqueue(new Callback<MessageFastModel>() {
                            @Override
                            public void onResponse(Call<MessageFastModel> call, Response<MessageFastModel> response) {
                                if (response.isSuccessful()) {
                                    Log.d("ddd", "onResponse: " + response.body().getData().size());
                                }
                            }

                            @Override
                            public void onFailure(Call<MessageFastModel> call, Throwable t) {
                                Log.e("ddd", "onFailure: " + t.getMessage());
                            }
                        });

but My error is

E/ddd: onFailure: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 44 path $.data

what's problem ? thanks!

  @GET("photos")
Call<Model> savePhoto();

And Call your retrofit method like this >>>

 Call<Model> call = apiService.savePhoto();

        call.enqueue(new retrofit2.Callback<Model>() {
            @Override
            public void onResponse(Call<Model> call, retrofit2.Response<Model> response) {
                int statusCode = response.code();
                try {
                    Model model;
                   // Log.w("resedatasync", new Gson().toJson(response));
                    model= response.body();

And Create Your Model Class like this

public class Model{

@SerializedName("ArrayName")
 public List<DataBean > arraylist= new ArrayList<>();

@SerializedName("message")
private String message;

public class DataBean {

    @SerializedName("id")
     private String id;

    @SerializedName("typename")
     private String typename;

    @SerializedName("typename")
    public void settypename(String typename) {
        this.typename= typename;
    }

}

}

When you have problem

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 44 path $.data

We must know that where need JSONArray .You have List<DataBean> data .So your data must use JSONArray .

And may be your problem at

Log.d("ddd", "onResponse: " + response.body().getData().size());

You can do like this .

System.out.println(response.body().string());

And show your message .

And try this class .

public class MessageFastModel {
    private boolean result;
    private String message;

    private DataBean data;

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public static class DataBean {
        private String id;
        private String type_name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getType_name() {
            return type_name;
        }

        public void setType_name(String type_name) {
            this.type_name = type_name;
        }
    }
}

And remove Log.d("ddd", "onResponse: " + response.body().getData().size());

This happens when the structure of JSON doesn't matches with the structure of your recipient model class. Also use @Expose and @SerializedName("receivingJsonVariableName") for each variable in the model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM