【问题标题】:Can't marshall firebase value properly无法正确编组 Firebase 值
【发布时间】: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


    【解决方案1】:

    我相信您必须在 Report 上的公共 getter 方法上使用 @PropertyName 注释,而不是私有字段名称。

    【讨论】:

    • 我实际上尝试过,但没有任何区别。作为一种解决方法,我重命名了字段和 getter/setter 以匹配 json 字段的拼写,例如 imageProfile 现在是 image_profile 并且有效,但违反了 Java 命名约定,所以我仍然宁愿使用注释..
    • 只是为了关闭它。我不确定是不是因为我使用的是旧版本的 Firebase,但是现在我使用的是最新版本的 Firebase,将注释放在字段名称、getter 和 setter 上似乎可以正常工作。
    猜你喜欢
    • 2016-09-13
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多