我在下面的代碼中出現了一個問題,它幾乎逐字地從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");
}
});
}
}
你的輸出是什麼? –
沒什麼,我甚至都不會看到「獲取價值之前」輸出。所以我不認爲這個功能甚至是解僱了。 –