我在下面的代码中出现了一个问题,它几乎逐字地从Firebase SDK Java文档中复制过来。我是一个真正的语言的新手,例如来自PHP和JavaScript中的webdev背景的Java。使用Java从Firebase中抓取数据
基本上addListenerForSingleValueEvent不会触发来给我回数据。我注意到这一点,因为系统打印输出不会触发,因此我认为听音事件没有触发。
我怀疑这与我有限的知识如何在函数本身的工作方式有关,比如我错过了一些关于类和函数如何相互作用的结构知识。
任何帮助,将不胜感激谢谢。
class FireBase {
public static void main(String[] args) throws IOException {
// Fetch the service account key JSON file contents
FileInputStream serviceAccount = new FileInputStream("key.json");
// Initialize the app with a service account, granting admin privileges
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://ssworks-adwords.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
// As an admin, the app has access to read and write all data, regardless of Security Rules
DatabaseReference ref = FirebaseDatabase
.getInstance()
.getReference("petitions");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("Before Get Value");
Object document = dataSnapshot.getValue();
System.out.println(document);
}
@Override
public void onCancelled(DatabaseError arg0) {
// TODO Auto-generated method stub
System.out.println("This didn't work");
}
});
}
}
你的输出是什么? –
没什么,我甚至都不会看到“获取价值之前”输出。所以我不认为这个功能甚至是解雇了。 –