【发布时间】:2019-07-13 09:56:19
【问题描述】:
我有一个应用程序可以写入 firebase 实时数据库上的数据库。我可以看到数据和它的罚款。
然后我只想读取数据,并自动将其编组到我的对象中。 JSON 有几层:
{
"subjects" : {
"reports" : {
"A3VDf5q3gXefuFdK98OikTQYKEu2" : { <--multiple ones of these
"report" : {
"email" : "email@email.com",
"profile_image" : "<LINK REMOVED>",
"question_one" : {
"answer" : "NO",
"question" : "Blah blah?",
"reason" : "meh"
},
"question_two" : {
"answer" : "NO",
"question" : "Meh Meh?",
"reason" : "bleh"
},
"time_stamp" : "130719003955"
}
}
}
}
}
(为了简洁和隐私,我删除了一些值)
在我的代码中,我可以手动提取数据:
final FirebaseAuth mAuth = FirebaseAuth.getInstance();
final FirebaseUser currentUser = mAuth.getCurrentUser();
final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("subjects").child("reports").child(currentUser.getUid());
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.d(TAG, "BEFORE CONVERSION::");
Log.d(TAG, "User UID::" + currentUser.getUid());
Log.d(TAG, "Children count::" + dataSnapshot.getChildrenCount());
Log.d(TAG, "Snapshot key::" + dataSnapshot.getKey());
Log.d(TAG, "Snapshot value::" + dataSnapshot.getValue());
Log.d(TAG, "Snapshot toString::" + dataSnapshot.toString());
Log.d(TAG, "email: " + dataSnapshot.child("report").child("email").getValue());
Log.d(TAG, "profile image: " + dataSnapshot.child("report").child("profile_image").getValue());
Log.d(TAG, "timeStamp: " + dataSnapshot.child("report").child("time_stamp").getValue());
Report report = dataSnapshot.child("report").getValue(Report.class);
Log.d(TAG, "Report: " + report.getProfileImage());
Log.d(TAG, "Report to string: " + report.toString());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e(TAG, "Failed to get value from DB", databaseError.toException());
}
});
Report report = dataSnapshot.child("report").getValue(Report.class); 之后的所有内容都是空的,除了 email 出于某种原因!!
显然,我想让它在我的模型中自动设置值:
public class Report {
@PropertyName("email")
private String email;
@PropertyName("profile_image")
private String profileImage;
@PropertyName("question_one")
private Question questionOne;
@PropertyName("question_two")
private Question questionTwo;
@PropertyName("time_stamp")
private String timeStamp;
public Report() {}
//Getters and setters
}
我也试过把监听器放在:
ref.child("report").addListenerForSingleValueEvent
但结果是一样的。
我做错了什么?
谢谢
【问题讨论】:
标签: android firebase firebase-realtime-database