0

I'm trying to write some data on my Realtime Database on Android Studio with Java. My goal is to create and user and then add user information in Realtime Database. I can create the User but when I'm trying to write on the Realtime Database nothing happen. I've add a onSuccess and onFailure listener to the function that set the data, but both of th listener doesen't run.

Code:

mAuth.createUserWithEmailAndPassword(user.getEmail(), user.getPassword())
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Toast.makeText(RegisterActivity.this, "Account creato!",
                        Toast.LENGTH_SHORT).show();


                FirebaseUser currentUser = mAuth.getCurrentUser();
                user.setUid(currentUser.getUid());
                //I add to RealTime Databse other User data
                DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
                mDatabase.child("users").child("test").setValue("hey").addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.i("Works","Yes");
                        Toast.makeText(RegisterActivity.this,"It orks",Toast.LENGTH_LONG).show();

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        // Write failed
                        Log.e("ErrorAdding",e.getMessage());
                        Toast.makeText(RegisterActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
                    }
                });

                //finish();

            } else {
                // If sign in fails, display a message to the user.
                Log.w("CREATEUSER", "createUserWithEmail:failure", task.getException());
                Toast.makeText(RegisterActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();

            }
        }
    });

I'm sure the user is created because the toast with "Account creato" shows up and in firebase I can see the account added.

This is my Realtime database rules:

    {
  "rules": {
    ".read": "true",  // 2022-2-4
    ".write": "true",  // 2022-2-4
  }
}

Thanks for your help!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Where is `user` variable defined? Why is the child data you're trying to set static for all users? – OneCricketeer Jan 05 '22 at 14:46
  • It's a parameter of this function. It's a class that contains email,password,username,uid – VondeTaconadis Jan 05 '22 at 14:47
  • You should ideally define `mDatabase` outside of the user creation callback. Besides that, `mDatabase.child("users").child("test").setValue` has nothing to do with user creation, so you should debug that in isolation. For example, does it correctly write the value on just a button click? – OneCricketeer Jan 05 '22 at 14:50
  • 1
    @OneCricketeer so I've tried it and it has the same previous behaviour. I've added a failure listener and it does nothing – VondeTaconadis Jan 05 '22 at 15:00
  • Does `users/test`path exist in the database? – OneCricketeer Jan 05 '22 at 15:01
  • @OneCricketeer No, I can't add empty field in realtime database. Does it work like firestore database where you can put the path and if it doesen't exist, it will make the missing folders? – VondeTaconadis Jan 05 '22 at 15:05
  • Paths are overwritten, if they exist, but if there's nothing there, shouldn't be a problem. Is there a specific reason you're using `"test"` rather than the user ID, as shown in the docs? https://firebase.google.com/docs/database/android/read-and-write – OneCricketeer Jan 05 '22 at 15:12
  • 1
    @OneCricketeer yes because at first I was using uid but it didn't work and for trying to see what was wrong I tryied a static path for check if it was the writing on Realtime database the problem – VondeTaconadis Jan 05 '22 at 15:16
  • 1
    If neither of the callbacks fires, it typically means the client is not connected to the database server. Can you check the logcat output for messages when you call `setValue` (or otherwise when you first access the database)? – Frank van Puffelen Jan 05 '22 at 15:18
  • @FrankvanPuffelen I've cheked the logcat when pressing the button but I don't see any message regarding the setValue or firebase – VondeTaconadis Jan 05 '22 at 15:28
  • Is any of the Toast messages displayed? – Alex Mamo Jan 05 '22 at 15:29
  • @FrankvanPuffelen so I added a value on the database manually and I tried to read it. I get this error: E/firebase: Error getting data java.lang.Exception: Client is offline . But I can create accounts on firestore so I don't know why it gives this error – VondeTaconadis Jan 05 '22 at 15:47
  • 1
    My guess is that you downloaded the `google-services.json` file before the database was created, which means the database URL that the SDK uses is incorrect. If that is indeed the cause, download the updated `google-services.json` and add it to your project, **or** put the database URL in your code in `FirebaseDatabase.getInstance("URL to your databse here").getReference()`. Also see: https://stackoverflow.com/a/69198094 – Frank van Puffelen Jan 05 '22 at 16:01
  • @FrankvanPuffelen yes, now I've changed the google-services.json with the new and inside the file I see the correct url to the database, but when in my application I try to log print it, it is still the previous one – VondeTaconadis Jan 05 '22 at 16:18
  • 2
    EDIT: I've cleaned and rebuilt my Android project and now it gives me the correct url. Now it works thanks – VondeTaconadis Jan 05 '22 at 16:25

0 Answers0