我正在嘗試使用Firebase構建社交應用。所以,基本上我有一個Post
類,這是類似於在火力地堡docs/guides喜歡/不喜歡Firebase的交易?
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);
但是,當我嘗試實施類似/不同的功能,如火力地堡文檔是這樣的
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()
哪些沒有工作。 我該如何解決這個問題?錯誤在這條線
編輯 -
棧跟蹤
Post p = mutableData.getValue(Post.class);
postTransaction:的onComplete:DatabaseError:從火力地堡數據庫runloop稱爲用戶代碼拋出異常: com.google.firebase。 database.DatabaseException:在com.google.android.gms.internal.zzamy $ zza上的類com.dark.confess.Post 上找不到序列化的屬性。(未知源) at com.google.android.gms.internal。 zzamy.zzj(未知來源) at com.google.android.gms.internal.zzamy.zze(Unknown Source) at com.google.android.gms.internal.zzamy.zzb(Unknown Source) at com.google.android.gms.internal。 zzamy.zza(Unknown Source) at com.google.firebase.database.MutableData.getValue(Unknown Source) at com.dark.confess.FireBaseHelper $ 5.doTransaction(FireBaseHelper.java:120) at com.google.android .gms.internal.zzajb.zza(Unknown Source) at com.google.firebase.database.DatabaseReference $ 4.run(Unknown Source) at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:423) at java.util.concurrent.FutureTask.r聯合國(FutureTask.java:237) 在java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 是java。 util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588) 在java.lang.Thread.run(Thread.java:818)
「get a n錯誤告訴我沒有可用的序列化數據「這聽起來出乎意料。你能分享確切的錯誤信息和堆棧跟蹤(指出你的代碼中的相關行號)嗎?如果您將代碼降低到重現問題所需的最小值,這也會有所幫助。例如,我懷疑身體,時間戳和作者是否與錯誤有關。從你的代碼中刪除它們,看看你是否仍然有問題,如果你這樣做,更新你的問題中的代碼。擁有更少(但仍然完整)的代碼使得它更容易回答。 –