6

I want to store users information by adding a key with its map object

private void createUserData() {
    Toast.makeText(LoginActivity.this, "test begin",
            Toast.LENGTH_SHORT).show();
    DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");

    //set fields data
    Map<String, String> userData = new HashMap<>();
    userData.put("userId", "test id ");
    userData.put("email", "as@gmail");
    userData.put("userName", "abdo");

    usersRef.child("userkey").setValue(userData);

    Toast.makeText(LoginActivity.this, "test end",
            Toast.LENGTH_SHORT).show();

}

when i click the button to trigger this code, the both Toast objects show up but no node added to users node. whats wrong with this code to write data to firebase database ?

Abdalltif Basher
  • 617
  • 7
  • 19
  • Most likely you're not authenticated and your security rules don't allow anonymous writes (since the default security rules for the dashboard only allow authenticated access). See the first blue box on the [page on saving data](https://firebase.google.com/docs/database/android/save-data) and [this answer](http://stackoverflow.com/questions/37403747/firebase-permission-denied/37404116#37404116). – Frank van Puffelen Jun 10 '16 at 20:34
  • in the dashboard a warning appears "Your security rules are defined as public, anyone can read or write to your database" { "rules": { ".read": true, ".write": true } } – Abdalltif Basher Jun 10 '16 at 20:44
  • Hmm... you might want to add a completion listener to see if the client consider the write "completed": https://firebase.google.com/docs/database/android/save-data#receive_a_completion_callback – Frank van Puffelen Jun 10 '16 at 21:13
  • I've added a completion listener but it never triggered. usersRef.child("userkey").setValue(newUser, new DatabaseReference.CompletionListener() { ... – Abdalltif Basher Jun 10 '16 at 21:18
  • do u already have 'userkey' node in the database? if so it will not uptade with same values. you should use variable keys or .push to auto generate keys. – ugur Jun 10 '16 at 21:27
  • I want to set the key node with its values programmatically, i don't want to use push method. the problem is the completion listener is not triggered at all to know if the data added or not!. – Abdalltif Basher Jun 10 '16 at 21:30
  • If your completion listener doesn't get triggered, your app is not connected to the Firebase servers. – Frank van Puffelen Jun 10 '16 at 21:58
  • I've tried usersRef.push().setValue(map) and it saved correctly but the callback not triggered. And i want to put the user's email as a key instead of a push. – Abdalltif Basher Jun 10 '16 at 23:05
  • 2
    I figured out that keys must not contain special characters, so thats why data are not being saved, But I still wondering why listener doesn't get triggered!. – Abdalltif Basher Jun 11 '16 at 01:23

1 Answers1

-1

1.change the <String, String> as <String, Object>

  Map<String, Object> userData = new HashMap<>();
  userData.put("userId", "test id ");
  userData.put("email", "as@gmail");
  userData.put("userName", "abdo");
    
  usersRef.child("userkey").updateChildren(userData);

2.change the firebase database get instance like FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance("https://yourdatabasename-default-rtdb.asia-southeast1.firebasedatabase.app")

recieved from this.

might be the database region problem like below

Firebase Database connection was forcefully killed by the server. Will not attempt reconnect. Reason: Database lives in a different region. Please change your database URL to https://databasename-default-rtdb.asia-southeast1.firebasedatabase.app

  • Please format your code/answer correctly and provide steps to effectively check that your hypothesis is correct. As it is, it looks like a stab in the dark. – santamanno Sep 27 '21 at 08:07
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '21 at 08:07