Firebase 评论查看错误 - 反序列化时需要一个列表,但得到了 class java.util.HashMap
Firebase Comments viewing error - Expected a List while deserializing, but got a class java.util.HashMap
当我尝试在列表中显示评论时出现此错误 -
com.google.firebase.database.DatabaseException:反序列化时需要一个列表,但得到了 class java.util.HashMap
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToParameterizedType(CustomClassMapper.java:251)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:177)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access100 美元(CustomClassMapper.java:48)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
我的评论显示 activity -
myRef.child("Photo")
.child(mphoto.getPhoto_id())
.child("comments")
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Log.d(TAG, "onChildAdded: child added.");
Query query = myRef
.child("Photo")
.orderByChild("photo_id")
.equalTo(mphoto.getPhoto_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dsnapshot) {
for ( DataSnapshot singleSnapshot : dsnapshot.getChildren()){
Photo photo = new Photo();
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
mComments.clear();
Comments firstComment = new Comments();
firstComment.setUser_id(mphoto.getUser_id());
firstComment.setComment(mphoto.getCaption());
firstComment.setDate_created(mphoto.getDate_Created());
mComments.add(firstComment);
List<Comments> commentsList = new ArrayList<Comments>();
for (DataSnapshot dSnapshot : singleSnapshot
.child("comments").getChildren()){
Comments comments = new Comments();
comments.setUser_id(dSnapshot.getValue(Comments.class).getUser_id());
comments.setComment(dSnapshot.getValue(Comments.class).getComment());
comments.setDate_created(dSnapshot.getValue(Comments.class).getDate_created());
mComments.add(comments);
}
photo.setComments(mComments);
mphoto=photo;
setupWidgets();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
错误说
Expected a List while deserializing, but got a class java.util.HashMap
这可能会出错
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
您可以更改它并使其变得简单,如下所示
String caption = singleSnapshot.child("caption").getValue(String.class);
photo.setCaption(objectMap.get("caption").toString());//So you wont able to do this
photo.setCaption(caption);//And you can use like this
而不是使用这个
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
换成这个
String caption=singleSnapshot.child("caption").getValue(String.class);
String tags= singleSnapshot.child("tags").getValue(String.class);
String photo_id=singleSnapshot.child("photo_id").getValue(String.class);
String user_id= singleSnapshot.child("user_id").getValue(String.class);
String date_Created = singleSnapshot.child("date_Created").getValue(String.class);
String image_Path= singleSnapshot.child("image_Path").getValue(String.class);
photo.setCaption(caption);
photo.setTags(tags);
photo.setPhoto_id(photo_id);
photo.setUser_id(user_id);
photo.setDate_Created(date_Created);
photo.setImage_Path(image_Path);
如您所见,没有objectMap
当我尝试在列表中显示评论时出现此错误 - com.google.firebase.database.DatabaseException:反序列化时需要一个列表,但得到了 class java.util.HashMap 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToParameterizedType(CustomClassMapper.java:251) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:177) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access100 美元(CustomClassMapper.java:48) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
我的评论显示 activity -
myRef.child("Photo")
.child(mphoto.getPhoto_id())
.child("comments")
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Log.d(TAG, "onChildAdded: child added.");
Query query = myRef
.child("Photo")
.orderByChild("photo_id")
.equalTo(mphoto.getPhoto_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dsnapshot) {
for ( DataSnapshot singleSnapshot : dsnapshot.getChildren()){
Photo photo = new Photo();
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
mComments.clear();
Comments firstComment = new Comments();
firstComment.setUser_id(mphoto.getUser_id());
firstComment.setComment(mphoto.getCaption());
firstComment.setDate_created(mphoto.getDate_Created());
mComments.add(firstComment);
List<Comments> commentsList = new ArrayList<Comments>();
for (DataSnapshot dSnapshot : singleSnapshot
.child("comments").getChildren()){
Comments comments = new Comments();
comments.setUser_id(dSnapshot.getValue(Comments.class).getUser_id());
comments.setComment(dSnapshot.getValue(Comments.class).getComment());
comments.setDate_created(dSnapshot.getValue(Comments.class).getDate_created());
mComments.add(comments);
}
photo.setComments(mComments);
mphoto=photo;
setupWidgets();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
错误说
Expected a List while deserializing, but got a class java.util.HashMap
这可能会出错
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
您可以更改它并使其变得简单,如下所示
String caption = singleSnapshot.child("caption").getValue(String.class);
photo.setCaption(objectMap.get("caption").toString());//So you wont able to do this
photo.setCaption(caption);//And you can use like this
而不是使用这个
Map<String, Object> objectMap = (HashMap<String, Object>) singleSnapshot.getValue();
photo.setCaption(objectMap.get("caption").toString());
photo.setTags(objectMap.get("tags").toString());
photo.setPhoto_id(objectMap.get("photo_id").toString());
photo.setUser_id(objectMap.get("user_id").toString());
photo.setDate_Created(objectMap.get("date_Created").toString());
photo.setImage_Path(objectMap.get("image_Path").toString());
换成这个
String caption=singleSnapshot.child("caption").getValue(String.class);
String tags= singleSnapshot.child("tags").getValue(String.class);
String photo_id=singleSnapshot.child("photo_id").getValue(String.class);
String user_id= singleSnapshot.child("user_id").getValue(String.class);
String date_Created = singleSnapshot.child("date_Created").getValue(String.class);
String image_Path= singleSnapshot.child("image_Path").getValue(String.class);
photo.setCaption(caption);
photo.setTags(tags);
photo.setPhoto_id(photo_id);
photo.setUser_id(user_id);
photo.setDate_Created(date_Created);
photo.setImage_Path(image_Path);
如您所见,没有objectMap