How to change firebase URL for FirebaseReference in Android Studio?

Viewed 849

i'm working on Login and register activity using Firebase that requires me to strore data in realtime database. I setup the firebase realtime database location in South East Asia, and this is the supposed URL: https://chatapp-5e8ce-default-rtdb.asia-southeast1.firebasedatabase.app/

and this is my code on writing Users data on the DB, after succesfully creating user using .createUserWithEmailAndPassword(email, password) ==>>

myRef = FirebaseDatabase.getInstance().getReference("Users");
String currUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
User user = new User(username, "default");

myRef.setValue(user)
       .addOnCompleteListener(new OnCompleteListener<Void>() {
           @Override
           public void onComplete(@NonNull Task<Void> task) {
               if (task.isSuccessful()){
                   
               } else{
                   
               }

           }
       });

After debugging, i found that the process fails at myRef.setValue(user) because the URL set up for database reference is https://chatapp-5e8ce-default-rtdb.firebaseio.com/Users instead of https://chatapp-5e8ce-default-rtdb.asia-southeast1.firebasedatabase.app/Users.

How do i set the URL so it match my firebase database location?

1 Answers

You can pass the URL to FirebaseDatabase.getInstance(), so:

myRef = FirebaseDatabase.getInstance("https://chatapp-5e8ce-default-rtdb.asia-southeast1.firebasedatabase.app").getReference("Users");

You can also download an updated google-services.json, which will contain the correct string and replace the existing (incompletely) file in your Android Studio project with that.

Related