java - 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;
}
}
解决方案
好的,我发现了什么问题..
我使用学生姓名作为 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 对象,用于保存新添加的学生数据。
推荐阅读
- python - 为什么 Python:requests.post 会导致 406 错误
- sql - 使用 SQL 在 Redshift 表中创建 SCD 历史记录
- html - CSS使图像以移动视图为中心,同时保持高度不变?
- mysql - 在 Spring Boot 应用程序中使用什么缓存来避免冗余数据库调用?
- bash - Bash - 找到文件后如何停止更深入的搜索?
- javascript - Angular 2+ 设置 ngModules 避免重复代码
- twilio - 在通过 webhook 收听时,Twilio 视频房间状态回调事件是否有任何订单?
- java - 在 Java 中查找对时降低时间复杂度
- python - 一组字符串的子集的索引
- javascript - 如何在 React 中的另一个 html 元素中嵌入 html 元素