I already write a simple code to write in the Realtime Database (Firebase) like the Documentation do. here’s the code
this code supposed add a "Hello, World".
But my database stays null.
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance("This is my url").getReference();
mDatabase.setValue("Hello, World").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("MainActivity", "Succes to write");
} else {
Log.e("MainActivity", "Failed to write", task.getException());
}
}
});
}
}
I already tried to check the rules about it need to be write "true" and read "true" (Its in test mode). And the log for the completion doesn’t show up
UPDATE: I solved it already, its my network that have problem, i already give my emulator permission to use Internet however my Wifi seems kinda weird it didn’t give connection to the emulator. I tried using hotspot and it worked. thanks
2
Answers
The first step is to add a completion listener as shown here and find if there’s any error coming back from the server.
If neither the success or the failure listener gets called, most likely your app is not correctly configured to find the database. A common cause of this is when you download the
GoogleServices.json
before you created the database in the Firebase console.There are two solutions in that case:
GoogleServices.json
and make a build of the app with that updated version.FirebaseDatabase database = FirebaseDatabase.getInstance("URL of your database");
dont forget
google-service.json
in your app directory level exist.configure your database rule, for example
Explanation:
rule
root
can be accessed without requiring login (public)rule
shared_prefs
it means for user that can only be accessed with login using firebase auth.lets asume you have database structure like:
lets declare data class for
root
, asume called asMyRoot.java
Here steps to get the db value:
put this on your application class
in your activity
the import sections is: