skip to Main Content

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


  1. 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:

    • Either you can redownload GoogleServices.json and make a build of the app with that updated version.
    • Or you can configure the URL inside the code of you app with FirebaseDatabase database = FirebaseDatabase.getInstance("URL of your database");
    Login or Signup to reply.
  2. dont forget google-service.json in your app directory level exist.

    configure your database rule, for example

    {
      "rules": {
        ".read": false,
        ".write": false,
        "root": {
          ".read": true,
          ".write": false,
          "url": {
            ".read": true,
            ".write": false
          }
        },
        "shared_prefs": {
          "$uid": {
            ".write": "$uid === auth.uid",
            ".read": "$uid === auth.uid"
          }
        }
      }
    }
    

    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:

    {
      "root": {
        "hello": "world"
      }
    }
    

    lets declare data class for root, asume called as MyRoot.java

    class MyJava {
       public String hello;
    }
    

    Here steps to get the db value:

    put this on your application class

    new FirebaseApp.initializeApp(context);
    

    in your activity

    // initialize database connection
    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    // get root data
    DatabaseReference rootRef = db.child("root");
    // we just read once, so we using addListenerForSingleValueEvent
    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot data) {
        MyRoot root = data.getValue(MyRoot.class);
        Log.d("my root data is " + Gson().toJson(root)); // my root data is {"hello":"world"}
      });
    });
    

    the import sections is:

    import com.google.firebase.auth.FirebaseUser;
    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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search