我在下面的代碼中遇到了一個問題,該代碼幾乎逐字地從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");
}
});
}
}