首页 > 解决方案 > DatabaseException:反序列化时需要一个列表,但得到一个类 java.util.HashMap

问题描述

我正在为教育学院开发一个应用程序,以下代码片段检索之前推送到 Firebase 实时数据库的 Parent 对象,以便在此 Student 对象也被推送到数据库之前链接到 Student 对象。

如果这是此特定父母的第一个学生(孩子),则代码可以正常工作。换句话说,此 Parent 对象具有他的孩子的列表。如果此列表为空或为空,则代码可以正常工作。但是如果要推送的学生是同一个父级的第二个孩子,这意味着检索到的 Parent 对象将包含一个子级列表,我得到这个:

com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap

这是检索父对象的方法:

private void getCorrespondingParent(){
    DatabaseReference correspondingParentReference = FirebaseDatabase.getInstance().getReference()
            .child("parents").child(mUserPhone);
    correspondingParentReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            mParent = snapshot.getValue(Parent.class);
            // null out the children field because the Parent object will be saved in the Student object
            // this avoids having a parent with children list that each Student in it has a parent object that
            // has a list of students...
            mParent.setChildren(null);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.w(LOG_TAG, "loadPost:onCancelled", error.toException());
        }
    });
}

数据库位于获取 dataSnapshot 值的行中: mParent = snapshot.getValue(Parent.class);

这是数据库中的父节点: DatabaseReference

这是父对象的 POJO 类:

public class Parent {
private int accType;
private String name;
private String uid;
private String phone;
private String whatsappPhone;
private String know;
private List<Student> children;

public Parent() {}

public Parent(String name, String uid, String phone, String whatsappPhone, String know) {
    this.accType = 3;
    this.name = name;
    this.uid = uid;
    this.phone = phone;
    this.whatsappPhone = whatsappPhone;
    this.know = know;
}

public int getAccType() {
    return accType;
}

public void setAccType(int accType) {
    this.accType = accType;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getWhatsappPhone() {
    return whatsappPhone;
}

public void setWhatsappPhone(String whatsappPhone) {
    this.whatsappPhone = whatsappPhone;
}

public String getKnow() {
    return know;
}

public void setKnow(String know) {
    this.know = know;
}

public List<Student> getChildren() {
    return children;
}

public void setChildren(List<Student> children) {
    this.children = children;
}

}

标签: javaandroidfirebasefirebase-realtime-database

解决方案


好的,我发现了什么问题..

父对象节点所示

我使用学生姓名作为 Student 对象的键,并使用 List 将父对象的子对象存储在 Parent 对象中。事实证明,如果您使用的是列表、列表,则不能为此目的使用自定义键项目键是它的索引,它不能被改变..我切换到Map<String, Student> children而不是List<Student> children现在它工作正常。

推送代码无需更改:

DatabaseReference newChildReference = FirebaseDatabase.getInstance().getReference()
            .child("parents").child(parentPhone).child("children").child(childKey);
    newChildReference.setValue(mNewStudent);
    newChildReference.push();

mNewStudent 是一个 Student 对象,用于保存新添加的学生数据。


推荐阅读