【问题标题】:Can't retrieve data From Firebase through Java Object Class(POJO)无法通过 Java 对象类(POJO)从 Firebase 检索数据
【发布时间】:2017-04-12 17:11:19
【问题描述】:

我无法将数据从 Firebase 提取到 Android。我正在使用 POJO(Java 对象类)方法来显示值。

这就是我在 Firebase 上整理数据的方式:

这是我的 POJO 类“MonitoringPojo.java”。

public class MonitoringPojo {

Results results;

public Results getResults(){

    return results;

}

public class Results {
    double MaxLat;
    double MaxLng;
    double  MinLat;
    double MinLng;
    String Congestion_Status;


    public double getMaxLat() {
        return MaxLat;
    }

    public double getMaxLng() {
        return MaxLng;
    }

    public double getMinLat() {
        return MinLat;
    }
    public double getMinLng() {
        return MinLng;
    }
    public String getCongestion_Status() {
        return Congestion_Status;
    }
}

}

这是我试图通过 POJO 类从 FireBase 获取和显示值的地方。

 MonitoringPojo pojo = dataSnapshot.getValue(MonitoringPojo.class);
            double sourceOne = pojo.results.getMaxLat();
            double destinationOne = pojo.results.getMaxLng();
            double sourceTwo= pojo.results.getMinLat();
            double destinationTwo= pojo.results.getMinLng();
            String fetchedColor=pojo.results.getCongestion_Status().toString();
            DrawPolygon2(sourceOne,destinationOne,sourceTwo,destinationTwo,fetchedColor);

但不幸的是,我什么也没显示。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: android google-maps firebase firebase-realtime-database


    【解决方案1】:

    为了能够从model 中检索数据,您需要创建类的the no argument constructor 以及每个变量的构造函数。此外,您还需要以与 getter 相同的方式提供公共 setter。

    一个例子是这样的:

    public void setMaxLat(long maxLat) {this.maxLat= maxLat;}

    您需要为所有变量设置此设置器。

    希望对你有帮助。

    【讨论】:

    • 我为类和setter方法添加了无参数构造函数。但现在我收到以下错误。 " E/UncaughtException: com.firebase.client.FirebaseException: 无法弹回键入 "
    • E/UncaughtException: com.firebase.client.FirebaseException: 无法弹回输入。
    • 引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“MinLng”(com.example.shahroze.sdsmap_v1.MonitoringPojo 类),未标记为可忽略(一个已知属性: “结果”])
    • 请阅读this 帖子以了解其工作原理。
    • 一切都好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多