Firebase Database points to wrong database URL

守給你的承諾、 提交于 2021-02-04 08:34:07

问题


I am working on an Android app and trying to use Firebase Database. I tried to write data to the Database but nothing was showing up (checked via Firebase console). I have Firebase libraries in the build:

implementation platform('com.google.firebase:firebase-bom:26.2.0')
implementation 'com.google.firebase:firebase-database'

My Firebase Database URL as per Firebease console is :

    https://xxxxxxxx-xxxxxxx-default-rtdb.europe-west1.firebasedatabase.app/

whereas when I get a reference to it from the code:

    private DatabaseReference mDatabase;

    mDatabase = FirebaseDatabase.getInstance().getReference();
    Log.i(TAG," Reference is: "+mDatabase.toString());

The logcat shows below response:

    I/Tag:  Reference is: https://xxxxxxxx-xxxxxxx-default-rtdb.firebaseio.com

I also have the google-services.json file in the project which points to the same database URL shown in the Firebase console. It appears to me that Firebase has misconfigured my database somewhere which I can't see / configure.

Is there anything I am missing?


回答1:


I recently faced a similar issue, Firebase URL is null even after updating google_services.json, I got it working by deleting the build folder of my app and rebuilding the project again




回答2:


I just tested this on a new project of my own with the exact same BoM version as you, and it works fine with just the google-services.json file and this code:

final DatabaseReference ref =  FirebaseDatabase.getInstance().getReference();
Log.i("firebase", ref.toString());
ref.child("from Android").push().setValue(ServerValue.TIMESTAMP);

And this runs without problems. The Log.i writes out:

I/firebase: https://realtime-database-in-brussels-default-rtdb.europe-west1.firebasedatabase.app

Which is indeed the correct URL for my database, and should be a URL pretty similar to yours.


I'm not sure why that doesn't work for you, but you can work around it by passing the correct URL into getInstance() explicitly. So:

mDatabase = FirebaseDatabase.getInstance("https://xxxxxxxx-xxxxxxx-default-rtdb.europe-west1.firebasedatabase.app/").getReference();


来源:https://stackoverflow.com/questions/65558463/firebase-database-points-to-wrong-database-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!