我在下面的代码中遇到了一个问题,该代码几乎逐字地从Firebase SDK Java文档中复制到了工作中.我是一个真正的语言的新手,比如来自PHP和JavaScript的webdev背景的Java.
基本上,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");
}
});
}
}