Expected A List While Deserializing, But Got A Class Java.util.hashmap
Solution 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:
Stringuid= FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
DatabaseReferenceanimalsRef= rootRef.child("users").child(uid).child("animals");
ValueEventListenervalueEventListener=newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
List<Animal> animalList = newArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Animalanimal= ds.getValue(Animal.class);
animalList.add(animal);
}
//Do what you need to do with the animalList
}
@OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
animalsRef.addListenerForSingleValueEvent(valueEventListener);
Solution 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...
Useruser= datasnapshot.getValue(User.class);
You need to create a User class that reflects your users attributes in the RTD
Solution 3:
The way you build your tree is the root of the problem.
I had the same error just today while refactoring a firebase tree of an old project, so I looked for the error in google and found this page, and I notice this 3 user attributes inside your animal node that looked out of place.
So I went back to my tree and apprantly I had mistakenly added a key:value pair inside one of my list nodes, so I removed them and everything was fine.
The problem:
It is because you have a list of animals and inside that list you have this 3 key-values (email, uuid, name), this confuses firebase serilization so he tries to convert it into hashmap
The solution:
I think you will benefit in the future if you don't work around it and do as follows: the 3 user attributes email, name and uuid should be wrapped inside a different node inside the user tree, something like user_data and you should have a different node for animals that will have only the animal list!
- users
- sfdasfasfasfasf
- user_data
- email:"asdf"
- name:"asdfas"
- uuid:"dfqwtr4"
- animals
- asdfaskdfjlasdgafd
- adult:"asdf"
- animalId:"asdfas"
- animalName:"dfqwtr
- approxAge:"asdf"
- neutred:"asdfas"
- photoLink:"dfqwtr"
- sdafasfdasgasga
- adult:"asdf"
- animalId:"asdfas"
- animalName:"dfqwtr
- approxAge:"asdf"
- neutred:"asdfas"
- photoLink:"dfqwtr"
- asdfaskdfjlasdgafd
- user_data
- sfdasfasfasfasf
Then your pojo's should look something like: a User
class with properties of a UserData
and List<Animal>
with the corresponding names for serilization.
Post a Comment for "Expected A List While Deserializing, But Got A Class Java.util.hashmap"