如何检索Firebase数据库?

时间:2018-07-07 08:36:27

标签: java firebase firebase-realtime-database

我无法从Java(非android)检索firebase数据库。我已经创建了Firebase数据库,如下所示:

enter image description here 代码

public static void main(String[] args) throws FileNotFoundException, IOException{
    FileInputStream serviceAccount = new FileInputStream("D:/xxxxxxxxxx-x9314-firebase-adminsdk-d8he2-4ab7b5976b.json");

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("https://xxxxxxxxx-x9314.firebaseio.com/")
            .build();

    FirebaseApp.initializeApp(options);

    DatabaseReference ref = FirebaseDatabase.getInstance()
        .getReference("cars");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
        Object car = dataSnapshot.getValue();
        System.out.println(car);
      }

      @Override
      public void onCancelled(DatabaseError error) {
      }
    });
}    

运行应用程序时没有数据出现。

请问有人可以帮我吗?

0 个答案:

没有答案