0

I want to get array of object (JSON) and then get some value.

I have tried done everything like here: How to get array of objects with gson/retrofit?

but I am having hint-error from Android Studio like 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
    }
}]

What I am doing in 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();
            }
        });
    }

In interface I have:

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

In User.class I have:

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

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

What Is wrong with my code? Where I did a mistake?

UPD:

Have tried:

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

Getting:

Expected BEGIN_ARRAY but was StRING at line 1 column 9 path $[0].id
danyapd
  • 2,516
  • 1
  • 14
  • 23

2 Answers2

2

It seems that response.body() returns a List<User> so you have to access to one item of the list. example:

response.body().get(0).getId();
Fartab
  • 4,725
  • 2
  • 26
  • 39
appersiano
  • 2,670
  • 22
  • 42
  • When I am doing this: `id = response.body().get(0).getId(); tvAccount.append(" " + id);` I am getting onFailure() method. But my API returning JSON in those format which I described in my question. – danyapd Apr 12 '19 at 15:25
  • Show the value of the Throwable on the OnFailure callback and also add your json sample... with for example...two object on the json array...are you sure that your json is not malformed? – appersiano Apr 13 '19 at 16:06
  • JSON is big and hard but I need only those first id value. – danyapd Apr 14 '19 at 14:47
  • Oh, and you asked about for two objects in example.. [JSON], [Same JSON structure but another values]. – danyapd Apr 14 '19 at 15:22
  • your model class is wrong, id is a simple String and not list, use this to generate the model class http://www.jsonschema2pojo.org/ – appersiano Apr 14 '19 at 20:20
0

you can not get getId() directly from the list

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

if you expect more than one user, you should go through the entire list of users to get the Id's

for (User user : listUser) 
{ 
    List<Integer> listIds=user.getId();
}

your object must be

public class User {
    private String id;

    public String getId(){
        return id;
    }

    public void setId(String id){
        this.id = id;
    }
}
yOshi
  • 820
  • 7
  • 12
  • I am not sure that I'm understanding what I need to do in my `onResponse()` and how I should get ID from JSON response – danyapd Apr 12 '19 at 15:32