嗨,我是Retrofit 2.0库的新手,解析某些json时遇到问题。我已经在Stackoverflow上查看了其他一些解决方案,但是对我的问题不太满意。我正在尝试使用翻新版2.0从android调用一个api,但是它抛出错误预期为BEGIN_ARRAY但为BEGIN_OBJECT。

我从Web 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": "[email protected]",
            "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();
            }
        });

    }

最佳答案

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

关于android - retrofit 2 throw 错误预期为BEGIN_ARRAY,但为BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36733142/

10-11 00:15