改造2投掷错误预期BEGIN_ARRAY但是BEGIN_OBJECT

时间:2016-04-20 03:12:30

标签: android retrofit

您好我是Retrofit 2.0库的新手,我在解析一些json时遇到了问题。我已经在Stackoverflow上看了一些其他的解决方案,但我的问题没有太多运气。我试图通过改装2.0从android调用api。但它抛出错误预期BEGIN_ARRAY但是BEGIN_OBJECT。

我对网络API的回复是:

{
    "items": [{
        "id": 19,
        "lat": 23.79418,
        "lng": 90.401859,
        "user_id": 1,
        "created_at": null,
        "updated_at": null,
        "user": {
            "id": 1,
            "name": "Tarif",
            "email": "fmt@itc.com",
            "created_at": null,
            "updated_at": null
        }

    }]
}

这就是我的Model类:

    public class UserResponse {
    private List<Items> items;
    public List<Items> getItems() {
        return items;
    }
    private class Items{
        private String id;
        private String lat;
        private String lng;
        private String user_id;
        private String created_at;
        private String updated_at;
        private List<User> user;

        private class User{
            private String id;
            private String name;
            private String email;
            private String created_at;
            private String updated_at;

            public String getId() {
                return id;
            }

            public String getName() {
                return name;
            }

            public String getEmail() {
                return email;
            }

            public String getCreated_at() {
                return created_at;
            }
            public String getUpdated_at() {
                return updated_at;
            }


        }
        public String getId() {
            return id;
        }
        public String getLat() {
            return lat;
        }
        public String getLng() {
            return lng;
        }
        public String getUser_id() {
            return user_id;
        }
        public String getCreated_at() {
            return created_at;
        }
        public String getUpdated_at() {
            return updated_at;
        }
        public List<User> getUser() {
            return user;
        }
    }
}

这是我的界面:

@GET("coordinates")
 Call <UserResponse> getUsers();

这就是我打电话的方式:

ITCService service = ApiUtils.createService(ITCService.class);
        Call<UserResponse> call= service.getUsers();
        call.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, retrofit2.Response<UserResponse> response) {

                if (response.isSuccessful()) {
                    UserResponse res = response.body();
                    Log.e("TAG", res.toString());

                } else {
                    // TODO: toast
                    Toast.makeText(getApplicationContext(),response.message(),Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                // TODO: error alert

                Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });

    }

1 个答案:

答案 0 :(得分:1)

List<User>应该HashMap<String,Object>不是列表,因为它是JSONObject而不是JSONArray。由于您在项目中定义了User类,因此只需使用User而不是List.No需要HashMap