反序列化时需要一个List,但是得到了一个类java.util.HashMap [英] Expected a List while deserializing, but got a class java.util.HashMap

查看:280
本文介绍了反序列化时需要一个List,但是得到了一个类java.util.HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Android的Firebase获取用户列表.

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

我已经有了用户的uuid,并使用它试图获取整个对象.uuid既是节点名,又是用户节点内的字段(当然是相同的).

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).

这是我的代码:

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) {

        }
    });

我不知道错误期望的列表但有HashMap"是来自整个DataSnapshot本身,还是来自用户内部的动物列表(用户类具有字段动物列表)

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.

我想获取用户电子邮件,因为它是ID(uuid).尽管不必获取整个User对象,但是我对如何完成操作感到好奇,因为我无法做到这两个目的.

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.

这是我的用户类

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
}

当尝试获取其中没有动物节点的User时,一切正常.但是当试图使用户进入动物节点时,应用程序崩溃了.

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.

推荐答案

实际上,我回答了这个问题,但是在这种情况下,问题有所不同.因此,您将收到以下错误:

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

预期列表,但获得了HashMap

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.

虽然没有必要获取整个User对象,但我对它的实现方式感到好奇,因为我无法做到这两个目的.

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);

现在,要将 DataSnapshot 对象映射到 User ,并获取特定用户的动物对象列表,请删除:

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;

并使用以下代码行:

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);

这篇关于反序列化时需要一个List,但是得到了一个类java.util.HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆