Firebase dataSnapshot未转换为自定义Java对象

时间:2017-02-06 07:23:59

标签: android firebase firebase-realtime-database

我正在尝试将从Firebase数据库检索到的JSON数据映射到项目中的自定义Java类。我正在尝试检索Restaurants节点下的所有子节点。

餐馆节点的Firebase数据结构:

"Restaurants" : {
    "ZyZnmC3xC39Jv1oVVdXbQdyCOcTQ" : {
      "restaurantContactNo" : "222922943",
      "restaurantImgUrl" : "https://firebasestorage.googleapis.com/...",
      "restaurantWebsite" : "www.bubbleme.com",
      "restaurant_address" : "40/1/1",
      "restaurant_category" : "coffee",
      "restaurant_latitude" : "6.983489f",
      "restaurant_longitude" : "79.932736f",
      "restaurant_name" : "Bubble Me Bubble Tea",
      "restaurant_rating" : 4.5
    }

以下是我尝试从检索到的数据中创建对象的方法:

mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Restaurants");
            mDatabaseRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            if(dataSnapshot.exists()){

                                for(DataSnapshot data : dataSnapshot.getChildren()) {

                                    Restaurants res = data.getValue(Restaurants.class);


    ......

res始终为空,我将数据快照打印到控制台,数据全部采用JSON格式:

{ZyZnmC3xC39Jv1oVVdXbQdyCOcTQ={restaurant_rating=4.5, restaurant_longitude=79.932736f, restaurant_category=coffee, restaurantContactNo=0112922943, restaurant_address=40/1/1, restaurantImgUrl=https://firebasestorage.googleapis.com/.., restaurant_name=Bubble Me Bubble Tea, restaurant_latitude=6.983489f, restaurantWebsite=www.bubbleme.com}}

餐馆Java类:

public class Restaurants {

    private String restaurant_longitude;
    private String restaurant_latitude;
    private String restaurant_name;
    private String restaurant_address;
    private String restaurantImgUrl;
    private String restaurant_category;
    private String restaurant_rating;
    private String restaurantContactNo;
    private String restaurantWebsite;

    public Restaurants(){}


    public Restaurants(String restaurant_rating, String restaurant_longitude, String restaurant_category, String restaurantContactNo,
                       String restaurant_address, String restaurantImgUrl, String restaurant_name, String restaurant_latitude, String restaurantWebsite) {

        this.restaurant_rating = restaurant_rating;
        this.restaurant_longitude = restaurant_longitude;
        this.restaurant_category = restaurant_category;
        this.restaurantContactNo = restaurantContactNo;
        this.restaurant_address = restaurant_address;
        this.restaurantImgUrl = restaurantImgUrl;
        this.restaurant_name = restaurant_name;
        this.restaurant_latitude = restaurant_latitude;
        this.restaurantWebsite = restaurantWebsite;

    }

    public String getRestaurantWebsite() {
        return restaurantWebsite;
    }

    public String getRestaurantContactNo() {
        return restaurantContactNo;
    }

    public String getRestaurantRating() {
        return restaurant_rating;
    }

    public String getRestaurantCategory() {
        return restaurant_category;
    }

    public String getRestaurantImgUrl() {
        return restaurantImgUrl;
    }

    public String getRestaurantAddress() {
        return restaurant_address;
    }

    public String getRestaurantTitle() {
        return restaurant_name;
    }

    public String getRestaurantLatitude() {
        return restaurant_latitude;
    }

    public String getRestaurantLongitude() {
        return restaurant_longitude;
    }

}

我已尝试多次使用ValueEventListnerChildValueEventListnerListnerForSingleChild,但我无法让它工作,我知道我非常接近,因为数据快照会检索数据但我相信将数据映射到Restaurants对象就是问题所在。

任何有关此方面的帮助都会有所帮助。

----问题固定----

我终于通过删除我的Restaurants Java类并使用所需成员重新实现该类来解决它。奇怪的是,新类与最后一个类完全相同但不知何故它起作用。也许是因为我的旧课程在我将Firebase集成到我的项目之前实施了很长时间,我想不出一个合理的原因它是如何工作的。

特别感谢Doug,Lewis和Rosário的宝贵意见。

1 个答案:

答案 0 :(得分:2)

正如您在评论中提到的那样,启用了ProGuard可能导致问题。

请参阅guidance here on configuring ProGuard,其中建议向proguard-rules.pro添加规则:

# Add this global rule
-keepattributes Signature

# This rule will properly ProGuard all the model classes in
# the package com.yourcompany.models. Modify to fit the structure
# of your app.
-keepclassmembers class com.yourcompany.models.** {
  *;
}

将Firebase的所有Java模型类放入一个包中,并更改keepclassmembers规则以匹配您的应用。基本上是因为您没有在代码中调用这些字段,ProGuard可以假设它们可以被删除,因此当Firebase尝试使用它们时它会失败。