我正在尝试使用Firebase构建社交应用。所以,基本上我有一个Post
类,类似于Firebase docs/guides
public class Post {
String uid;
String timeStamp;
String author;
String body;
int starCount = 0;
Map<String, Boolean> stars = new HashMap<>();
public Post() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Post(String uid, String author, String body, String timeStamp) {
this.uid = uid;
this.author = author;
this.body = body;
this.timeStamp = timeStamp;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("uid", uid);
result.put("author", author);
result.put("body", body);
result.put("starCount", starCount);
result.put("stars", stars);
result.put("time", timeStamp);
return result;
}
}
我使用相同的writeNewPost()
方法来保存新帖子,但由于stars
hashmap最初为空,因此明星子节点永远不会在“posts”节点中创建为子节点
稍后当我尝试使用postRef.getValue(Post.class)
或postRef.setValue(Post.class)
时,我收到错误消息,告诉我没有可用的可序列化数据。
我尝试了明显的解决方法并试图做到这一点完全正常 -
//this didn't work
//Post post = postSnapShot.getValue(Post.class);
Post post = new Post();
post.uid = postSnapShot.child("uid").getValue(String.class);
post.body = postSnapShot.child("body").getValue(String.class);
post.starCount = postSnapShot.child("starCount").getValue(Integer.class);
post.timeStamp = postSnapShot.child("time").getValue(String.class);
post.author = postSnapShot.child("author").getValue(String.class);
但是,当我尝试实现类似/不同的功能时,如Firebase文档中再次提到的那样,就像这样
public boolean likeOrUnlikePost(String postId, final String uid) {
DatabaseReference postRef = databaseReference.child(Constants.POSTS).child(postId);
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
//Post p = mutableData.getValue(Post.class);
// above line didn't work again, so used the same workaround
Post post = new Post();
post.uid = mutableData.child("uid").getValue(String.class);
post.body = mutableData.child("body").getValue(String.class);
post.starCount = mutableData.child("starCount").getValue(Integer.class);
post.timeStamp = mutableData.child("time").getValue(String.class);
post.author = mutableData.child("author").getValue(String.class);
post.stars = mutableData.child("stars").getValue(Map.class);
if (post == null) {
return Transaction.success(mutableData);
}
if (post.stars.containsKey(uid)) {
// Unstar the post and remove self from stars
post.starCount = post.starCount - 1;
post.stars.remove(uid);
} else {
// Star the post and add self to stars
post.starCount = post.starCount + 1;
post.stars.put(uid, true);
}
// below line didn't work as well
// mutableData.setValue(post);
//so I tried this which doesn't work as I assumed it to work.
mutableData.child("starCount").setValue(post.starCount);
mutableData.child("stars").setValue(post.stars);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
Log.d(TAG, "postTransaction:onComplete:" + databaseError);
}
});
return true;
}
所以,我坚持使用mutableData.setValue()
无法正常工作。
我该如何解决这个问题?
编辑:
行上错误的Stacktraces
Post p = mutableData.getValue(Post.class);
postTransaction:onComplete:DatabaseError:从Firebase数据库runloop调用的用户代码引发了异常: com.google.firebase.database.DatabaseException:没有在类com.dark.confess.Post上找到要序列化的属性 在com.google.android.gms.internal.zzamy $ zza。(未知来源) 在com.google.android.gms.internal.zzamy.zzj(未知来源) 在com.google.android.gms.internal.zzamy.zze(未知来源) 在com.google.android.gms.internal.zzamy.zzb(未知来源) 在com.google.android.gms.internal.zzamy.zza(未知来源) 在com.google.firebase.database.MutableData.getValue(未知来源) 在com.dark.confess.FireBaseHelper $ 5.doTransaction(FireBaseHelper.java:120) 在com.google.android.gms.internal.zzajb.zza(未知来源) 在com.google.firebase.database.DatabaseReference $ 4.run(未知来源) at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:423) 在java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588) 在java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:0)
No properties to serialize
方法中的 DataSnapshot.getValue()
表示您错过了Post
类字段的getter
public class Post {
private String uid;
private String timeStamp;
private String author;
private String body;
private int starCount;
private Map<String, Boolean> stars;
public Post() {
}
public String getUid() {
return uid;
}
public String getTimeStamp() {
return timeStamp;
}
public String getAuthor() {
return author;
}
public String getBody() {
return body;
}
public int getStarCount() {
return starCount;
}
public Map<String, Boolean> getStars() {
return stars;
}
}