0

I have a JsonObjectRequest Get Method (volley), it was working fine, I got an JsonObject and used it for further actions but now it doesn't do what I want..

In the same class like the Get Request, I have the Method processResponse, which creates an "Account" Object and adds it to the Manager List:

private void processResponse(JSONObject response) {
    try {
        Log.e("processResponse", " called");
        final Account user = new Account("","","");

        String username = response.getString("Username");
        String email = response.getString("Email");
        String id = response.getString("Id");
        user.setUsername(username);
        user.setEmail(email);
        user.setId(id);

        Manager.AddObjectToUserList(user);

Manager Class with List:

class Manager {

private static List<Account> _ListofUsers = new ArrayList<Account>();

static void AddObjectToUserList(Account acc)
{
    _ListofUsers.add(acc);
}

List<Account> getListOfUsers(){
    return _ListofUsers;
}
}

I get an JsonObject from the GetResponse, it does successfully process the Response and creates an Object but when i want to return the List in Manager to another class, the "return List" is 0, I don't get the problem, since it was working fine before.

In debug mode, it jumps after the return List into the Looper.java and it doesn't leave this class..

1 Answers1

0

This is happening because of asynchronous behaviour. You are trying to add the object to list after you are creating the list, that's why you are getting 0. If you want to use that list, you need to use it in processResponse method. Please see also this post

Hope it helps.

Community
  • 1
  • 1
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I tried to do so like on this post but I dont know how to use VolleyCallback right :( If I do so like there, callback can't be found and i don't know where i should implement VolleyCallback right –  May 03 '17 at 07:30
  • `VolleyCallback` it's a public interface, but can call it in any way you want. I suggest you for another approach, to visit this [post](http://stackoverflow.com/questions/43576441/querying-data-from-firebase) and this [post](http://stackoverflow.com/questions/33723139/wait-firebase-async-retrive-data-in-android). Please keep me posted. – Alex Mamo May 03 '17 at 10:54
  • sorry for not replying and yea but now i have Another problem, it seems like the app doesnt wait for the servers response wheb i send a get request, list of the objects is empty and when i debug i see that the remaining code processed and my server response kust arrived. –  May 07 '17 at 20:20
  • Please take a look at this [post](http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener) too and keep me posted. – Alex Mamo May 08 '17 at 09:06
  • I switched back to a older version of the app, I havn't found the issue, code is the same but it works, i thank you very much for your help :) –  May 11 '17 at 12:45
  • Good to hear you figure it out. If you think that my answer helped you, please consider accepting it. Thanks! – Alex Mamo May 11 '17 at 12:48