'Firebase Realtime Database from Android setValue not working

I am trying to use the realtime database for the first time. The set value function is not working for me.

I already updated the writing rules in the following way :

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

My code looks like this:

        Integer Age = 24;
        FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
        DatabaseReference sReference = mDatabase.getReference();
        sReference.child("Users").child("George").child("Age").setValue(Age)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(Dashboard.this, "Data stored", Toast.LENGTH_LONG).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(Dashboard.this, "Error", Toast.LENGTH_LONG).show();
                    }
                });

When I print out the reference it is correct. However neither the OnSucces nor the OnFailure is triggered.

Thank you for your help.



Solution 1:[1]

Found the problem. The database is located in Europe. Apparently I did not specify the URL in the getInstance() method, which seems to be required in this case.

Solution 2:[2]

This is probably a firebase realtime database bug. It happens when you select Europe server (beta) instead of USA. And occures with no errors or warning of any kind.

Fix as mentioned above. Use direct link from firebase console in you database instance (change xxx with your project data from firebase console realtime database section):

// Write a message to the database
val database = Firebase.database("https://xxxxxxxx-xxxxxxx-default-rtdb.europe-west1.firebasedatabase.app/")
val myRef = database.getReference("message")

myRef.setValue("Hello, World!")

Solution 3:[3]

  1. Ensure your sha1 is added to firebase
  2. Enable signIn method in Firebase
  3. Enable Android Device Verification in Google Cloud
  4. Ensure internet permission in AndroidManifest.xml
  5. Provide db url in getInstance(). How to find Database URL
    DatabaseReference ref = FirebaseDatabase.getInstance("<<Insert Databse URL here>>").getReference();
    
  6. Restart your emulator and modem/router
  7. debug with logs

Solution 4:[4]

Please make sure you are specifying database URL while getting database instance like -

val database = Firebase.database("<url_here>")
val ref = database.reference

and then perform setValue() function over it say -

ref.child("users").child(uid).setValue(user)

This worked in my case!

Solution 5:[5]

I've had the same problem and I got the answer hope it's not too late for others.....

  1. After Creating Realtime database go to Project Settings in firebase dashboard -> Project Overview -> Gear Icon -> Project Settings and scroll down and check google-services.json file and download it again and replace with same place in android studio -> select project view -> in app folder (you know the drill) .... and clean - rebuild app again...... it worked for me after hours.....

  2. in short just redownload and replace google-services.json again and clean - rebuild project....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Both
Solution 2 sanevys
Solution 3 Elson Jose
Solution 4 Shantnu Kumar
Solution 5 Aditya Palange