Expected a List while deserializing, but got a class java.util.HashMap

假装没事ソ 提交于 2020-07-22 21:36:46

问题


I'm trying to get a list of users from Firebase, in Android.

I already have the uuid of the user, and using that I'm trying to get the entire object. The uuid is both the node name and a field inside the user node (identical, of course).

Here is my code:

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
Query query = usersRef.orderByChild("uuid").equalTo(animalMarker.getUserUid());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Log.d(TAG, "onDataChange: xxxxxxxx---- entered onDataChange");

            // go to node "users/userUID" , becuase dataSnapshot is parent node
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                Log.d(TAG, "xxxxxxxx---- onDataChange: ds= " + ds);
                User user = ds.getValue(User.class);
                Log.d(TAG, "xxxxxxxx---- onDataChange: user=" + user);
                // createdByTV.setText("Created by \n" + user.getEmail());
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

I dont know if the error "expected List but got HashMap" is coming from the entire DataSnapshot itself, or from the animals List inside the user ( The user class has a field List animals )

I've already checked this similar question Andorid-Expected a List while deserializing, but got a class java.util.HashMap and several others but I still can't understand what is wrong.

EDIT: I'm trying to get the user email, knowing it's ID(uuid). While getting the entire User object is not necessary, I'm curios as to how it would be done, since I can't manage to do either of these.

EDIT: here is my User class

public class User {

private String uuid;
private String email;
private String name;
private List<User> friends;
private List<Animal> animals;

public User() {
}

public User(String email, String name) {
    this.email = email;
    this.name = name;
}

public User(String uuid, String email, String name) {
    this.uuid = uuid;
    this.email = email;
    this.name = name;
}
... // getters and setters, toString and other such
}

When trying to get the User which doesn't have the animals node inside it, everything works fine. but when trying to get the user with the animals node inside, the app crashes.


回答1:


Actually, I answered that question but in this case the problem is a little different. So you are getting the following error:

expected List but got HashMap

Because your animals node that exist within each user object is not a list of animal objects is actually a map of animal objects and that's why this error.

While getting the entire User object is not necessary, I'm curios as to how it would be done, since I can't manage to do either of these.

It's true, you can get the email address simply using the following line of code:

String email = ds.child("email").getValue(String.class);

Now, to map a DataSnapshot object to a User and get a list of animal objects from a particular user, please remove:

//private List<User> friends;
//private List<Animal> animals;

And use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference animalsRef = rootRef.child("users").child(uid).child("animals");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<Animal> animalList = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Animal animal = ds.getValue(Animal.class);
            animalList.add(animal);
        }

        //Do what you need to do with the animalList
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
animalsRef.addListenerForSingleValueEvent(valueEventListener);



回答2:


The animals list is not inside the user, it is inside the users node. In any case making it inner to the user object seems to be more nested than needed. You should have another node for the animals

user_animals:{
    uid:{key1:{}, key2:{}
}

Your problem now is you are making a query instead of getting the node value

usersRef.child(uid).addSingleEvent..

...onDataChange...
User user = datasnapshot.getValue(User.class);

You need to create a User class that reflects your users attributes in the RTD



来源:https://stackoverflow.com/questions/55694354/expected-a-list-while-deserializing-but-got-a-class-java-util-hashmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!