首页 文章

Firebase getValue()永远不会结束

提问于
浏览
0

我遇到了 Firebase Admin with Java的问题 . 我使用Firebase实时数据库,我必须读取数据库中特定子项的数据 . 问题是我使用 ValueEventListener 来读取数据,但 onDataChange 中的 getValue() 永远不会结束或永远不会执行 . 我试图在我的模型中的特定对象中转换DataSnapshot(其中非基本对象作为属性) . 我想这样做:

System.out.println(dataSnapshot.getValue(MyObject.class).toString());

但没有打印出来!我也尝试过这个调用(然后使用,例如,gson):

System.out.println(dataSnapshot.getValue(JSONObject.class).toString());

但没有再打印出来 . 此代码位于我的ValueEventListener的onDataChange中 . 我也尝试打印元素的所有单个属性的孩子,我没有问题,但如果我尝试调用getValue(MyObject.class)没有任何反应 . 我和 Spring 天在Heroku上 .

哪个是我的错?

[编辑]:这是我要检索的数据库中的属性:

"ricette" : {
    "-L2yUEiCOkxn4zLISjOm" : {
      "a" : true,
      "approvata" : 1516098247226,
      "autore" : {
        "displayname" : "Matteo",
        "id" : "CaOUusrQLFQDW0rvnC0xkiEBSv42"
      },
      "carboidrati" : 42.584,
      "chiave" : "-L2yUEiE-Zos_y1GcRrv",
      "id" : "-L2yUEiCOkxn4zLISjOm",
      "image" : "url_to_image",
      "ingredienti" : [ {
        "alimento" : {
          "carboidrati" : 1.4,
          "cod" : "1316_2",
          "id" : "9506094d-9986-46a2-b825-c5ad174b936e",
          "kcal" : 57,
          "lipidi" : 1,
          "nome" : "POLPO",
          "proteine" : 10.6,
          "ratio" : 0.08
        },
        "quantita" : 1000
      }, {
        "alimento" : {
          "carboidrati" : 11.4,
          "cod" : "100180_1",
          "id" : "0b520eee-fada-44c6-9544-0072bd8634bc",
          "kcal" : 55,
          "lipidi" : 0.4,
          "nome" : "POMODORO, CONCENTRATO (sostanza secca 18%)",
          "proteine" : 0.9,
          "ratio" : 0.03
        },
        "quantita" : 250
      }, {
        "alimento" : {
          "carboidrati" : 8.4,
          "cod" : "301_1",
          "id" : "44f1b140-b40d-436c-80c7-0ba336c64045",
          "kcal" : 45,
          "lipidi" : 0.6,
          "nome" : "AGLIO, fresco",
          "proteine" : 0.9,
          "ratio" : 0.06
        },
        "quantita" : 1
      }, {
        "alimento" : {
          "carboidrati" : 0,
          "cod" : "338_1",
          "id" : "7077bfc1-bc63-48ea-8647-c596da34416f",
          "kcal" : 30,
          "lipidi" : 0.6,
          "nome" : "PREZZEMOLO, fresco",
          "proteine" : 3.7,
          "ratio" : 0.16
        },
        "quantita" : 10
      } ],
      "inviata" : 1516098091939,
      "kcal" : 710.95,
      "lipidi" : 11.066,
      "nome" : "Polpo in umido",
      "note" : "Nota sul polpo in umido",
      "portata" : "Secondo",
      "proteine" : 108.629,
      "ratio" : 0.07
    }

这些是我的模型中的Java类:

public class Ricetta implements Serializable {

        private User.Autore autore;
        private String id;
        private String nome;
        private String portata;
        private String note;

        private ArrayList<Alimento.Ingrediente> ingredienti;

        private double lipidi;
        private double proteine;
        private double carboidrati;
        private double kcal;
        private double ratio;
    }

    public class Alimento implements Serializable {

        public Alimento() {
        }

        private String id;
        private String cod;
        private String nome;
        private boolean aggiuntodautente;

        private double lipidi;
        private double proteine;
        private double carboidrati;
        private double kcal;
        private double ratio;
    }



    public class Ingrediente implements Serializable {

        public Ingrediente() {
        }

        private Alimento alimento;
        private int quantita;
}

    public class Autore implements Serializable {

        public Autore() {
        }

        public Autore(String displayname, String id) {
            this.setDisplayname(displayname);
            this.setId(id);
        }

        private String displayname;
        private String id;
}

这是对象,特别是:

public class RicettaG extends Ricetta implements Serializable {

    private String chiave;
    private String image;

    private long inviata;
    private boolean a;
    private long approvata;

}

(使用所有getter和setter)问题在于引用 database.getReference("ricette/-L2yUEiCOkxn4zLISjOm") 中的 dataSnapshot.getValue(RicettaG.class) 调用 . 也许问题是在数据库中不存在属性 aggiuntodautente ?谢谢 .

1 回答

  • 0

    当您使用以下代码行时:

    System.out.println(dataSnapshot.getValue(MyObject.class).toString());
    

    显然,您正在尝试从数据库中打印数据,但事实并非如此 . 您正在获取模型类的对象,然后将其转换为没有意义的String,因为您没有覆盖模型类中的 toString() 方法 . 您实际打印的内容是来自内存的 MyObject 对象的地址 .

    使用以下代码行时:

    System.out.println(dataSnapshot.getValue(JSONObject.class).toString());
    

    也错了 . 您无法在 dataSnapshot 对象上使用 getVaue() 方法获取 JSONObject 类型的对象 . 再次,将它转换为String它根本没有任何意义 .

    假设您的 ricette 节点是Firebase根目录的直接子节点,请使用以下代码:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ricetteRef = rootRef.child("ricette");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                Ricetta ricetta = ds.getValue(Ricetta.class);
                System.out.println(ricetta.getNome());
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    ricetteRef.addListenerForSingleValueEvent(eventListener);
    

    输出将是 nome 属性的所有值 .

相关问题