那麼我使用netbean,這是我的code.But當我運行的代碼沒有錯誤,沒有數據保存在firebase.I已經做到了這一點的幫助下谷歌firebsae教程https://firebase.google.com/docs/database/admin/save-data。 那麼,什麼是錯誤的,我code.Or是否有任何其他好的方法來做到這一點如何將數據從java應用程序保存到firebase
package com.recomm.mavenrec.algorithm.firebaseConnection;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.recomm.mavenrec.Entity.Book;
import com.recomm.mavenrec.Entity.Hospital;
import java.io.FileInputStream;
import java.io.FileNotFoun## Heading ##dException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FirebaseConnection {
public void initiainzeSDK() {
FileInputStream serviceAccount;
try {
serviceAccount = new FileInputStream("myJson.json");
FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://adsnavigator-bc363.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
} catch (FileNotFoundException foe) {
System.out.println(foe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
public void authrnticate() {
DatabaseReference ref = FirebaseDatabase
.getInstance()
.getReference("restricted_access/secret_document");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object document = dataSnapshot.getValue();
System.out.println(document);
}
@Override
public void onCancelled(DatabaseError error) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
}
public void writeTOFb() {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("rec");
System.out.println(ref);
DatabaseReference usersRef = ref.child("abc");
System.out.println(usersRef);
Map<String, Hospital> users = new HashMap<>();
users.put("alanisawesome", new Hospital("abc", "sfsf", 1f));
users.put("gracehop", new Hospital("xyz", "rde", 2f));
System.out.println(users);
usersRef.setValue(users);
}}
`
這是我的數據庫規則{ 「規則」:{ 「.read 」: 「AUTH = NULL」, 「 .WRITE」: 「AUTH = NULL」 }} 你 –
所連接的管理不需要特權讀或寫,但試圖改變規則,以'{ 「規則」:{ 「.read‘:真實, ’.WRITE」:真 }} ' 弟兄 –
我只是改變了規則,但仍然是同樣的問題。現在我能做什麼? –