Home > front end >  Fail to Write data to Firebase Realtime Database Android
Fail to Write data to Firebase Realtime Database Android

Time:09-16

I have created a Firebase, and I can write data into Authentication. But, I could not write data into realtime database.

  1. I have connected to Firebase enter image description here
  2. I have add Realtime Database
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.4.1')
// Declare the dependency for the Realtime Database library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-database:20.0.2'
  1. I have set rules of read and write to ture
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

I tried this to write data into Firebase but I get noting enter image description here

// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
// Write a message to the database
FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");
        FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");

CodePudding user response:

The code for writing looks fine, so I think the problem may be elsewhere.

If you created the Firebase console after you downloaded the google-services.json file, that file will not contain the correct URL, and the SDK may not be able to find your database on the server.

To solve that problem, you can either:

  • Download an updated google-services.json and use that in your app, or
  • Provide the database URL in your code with FirebaseDatabase database = FirebaseDatabase.getInstance("database URL here");

CodePudding user response:

My code works this morning, but I did nothing except go to bed. As shown in the picture, the second step has changed. I clicked that Add the Realtime Database SDK to your app button thousands of times, and it was still a button. Surprisingly, it has changed to a checked state.

I did the google-services.json part as @FrankvanPuffelen said yesterday, but it didn't work.

I have restarted the Android Studio a lot of times and didn't work.

Maybe I should try close the computer lid next time.

But now I know that one should have the second step checked rather than keep it as a unchecked button.

enter image description here

  • Related