在通过改造获取对象数组时,错误无法解析方法

问题描述 投票:0回答:2

我想得到对象数组(JSON),然后得到一些值。

我尝试过像这里做的所有事情:How to get array of objects with gson/retrofit?

但是我有来自Android Studio的提示错误,如Cannot resolve method 'getId()'

JSON:

[{
    "id": "551454",
    "rights": ["auth", "objects-prepaid-get"],
    "services": [{
        "service_id": "103",
        "options": {
            "sum": 0
        },
        "dismisable": 0
    }],
    "date_activate": "2017-11-11",
    "period": {
        "date_action": "2019-03-29",
        "after": 0
    },
    "ast": {
        "id": "3",
        "services": [{
            "service_id": "4",
            "dismisable": 0
        }, {
            "service_id": "5",
            "dismisable": 0
        }]
    },
    "ast_s": {
        "id": "82",
        "services": [{
            "service_id": "4",
            "dismisable": 0
        }, {
            "service_id": "5",
            "dismisable": 0
        }],
        "date": "2019-03-29",
        "dissmissable": 1
    }
}]

我在Activity中做了什么:

onCreate(){
...
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        SomeAPI userClient2  = retrofit.create(SomeAPI.class);
        getUserInfo();

}

private void getUserInfo() {
        token = AppPreferences.getStringFromSh(getApplicationContext());
        Call<List<User>> call2 = userClient2.getInfoUser("Bearer " + token);

        call2.enqueue(new Callback<List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {
                if (response.code() == 200){
                        id = response.body().getId(); //id - List<Integer>
                        tvAccount.append(" " + id);

                }
                else {
                    Toast.makeText(UserProfile.this, "Err while loading", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Toast.makeText(UserProfile.this, "error!", Toast.LENGTH_SHORT).show();
            }
        });
    }

在接口我有:

@GET("objects")
    Call<List<User>> getInfoUser(@Header("Authorization") String token);

在User.class我有:

public class User {
    private List<Integer> id;
 public List<Integer> getId(){
        return id;
    }

    public void setId(List<Integer> id){
        this.id = id;
    }
}

我的代码出了什么问题?我在哪里犯了错误?

UPD:

试过:

            id = response.body().get(0).getId();
            tvAccount.append(" " + id);

获得:

Expected BEGIN_ARRAY but was StRING at line 1 column 9 path $[0].id
android json list retrofit
2个回答
2
投票

似乎response.body()返回List<User>所以你必须访问列表中的一个项目。例:

response.body().get(0).getId();

0
投票

你不能直接从列表中获取getId()

List<User> listUser=response.body();

如果您希望有多个用户,则应浏览整个用户列表以获取Id

for (User user : listUser) 
{ 
    List<Integer> listIds=user.getId();
}
© www.soinside.com 2019 - 2024. All rights reserved.