Well I m using netbean and this is my code.But when I run the code there is no error and no data is saving in the firebase.I have done this by the help of google firebsae tutorial https://firebase.google.com/docs/database/admin/save-data. So What's wrong in my code.Or Is there any other good method to do this
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);
}}
`
You probably forgot to change the database write rules to ".write": true
you can add this and check what it the loged error.
usersRef.setValue(users, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
System.out.println("Data could not be saved " + databaseError.getMessage());
} else {
System.out.println("Data saved successfully.");
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With