预计BEGIN_ARRAY但是BEGIN_OBJECT改造2

时间:2017-11-17 09:05:59

标签: java android retrofit2

我有一些关于retrofit2的问题

如何修复预期的BEGIN_ARRAY,但在改造中是BEGIN_OBJECT

我不知道要解决它

我错了什么?

我的json是

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

我的模型类是

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;
        }
    }
}

我的代码是

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

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());
                            }
                        });

但我的错误是

  

E / ddd:onFailure:java.lang.IllegalStateException:预期   BEGIN_ARRAY但在第1行第44栏路径为$ .data

的BEGIN_OBJECT

有什么问题?谢谢!

3 个答案:

答案 0 :(得分:1)

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

并调用您的改造方法,例如&gt;&gt;&gt;

 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();

并像这样创建你的模型类

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;
    }

}

}

答案 1 :(得分:0)

遇到问题时

  

java.lang.IllegalStateException:预期为BEGIN_ARRAY但在第1行第44列BEGIN_OBJECT路径$ .data

我们必须知道需要JSONArray。您有List<DataBean> data。因此,您的数据必须使用JSONArray

可能是你的问题

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

你可以这样做。

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

并显示您的信息。

试试这堂课。

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;
        }
    }
}

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

答案 2 :(得分:0)

当JSON的结构与收件人模型类的结构不匹配时,会发生这种情况。还要为模型类中的每个变量使用@Expose@SerializedName("receivingJsonVariableName")