0

I have searched all the questions regarding this problem on Stack Overflow and I was not able to get the correct answer. The answers posted in the answer section or comment sections were not working for me. I have tried to log the values but addValueEventListener is not triggering. However, I can see the correct database reference (toString) just before the the addValueEventListener function in the logs. I have allowed all users to the database so there shouldn't be an authentication issue. I tried adding this to MainActivity.class as well but that doesn't seem to work. I have SHA1 fingerprint on the database and there is a connection to the database as well since my log.d prints the database reference child value.

Rules for my database are as follows:

{
  "rules": {
    ".read": "auth == null",
    ".write": "auth == null",
  }
}

ReadWriteActivity.class

package com.example.grocery;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.*;

public class ReadWriteActivity extends AppCompatActivity {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_write);

        listView = findViewById(R.id.listView);

        final ArrayList<String> list = new ArrayList<>();
        final ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
        listView.setAdapter(adapter);

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("names");
        Log.d("DATABASE REFERENCE", reference.toString());
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                list.clear();
                for (DataSnapshot datasnapshot : snapshot.getChildren()) {
                    Log.d("KEY VALUE", datasnapshot.getValue().toString());
                    list.add(datasnapshot.getValue().toString());
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.w("KEY ERROR", "Failed to read value.", error.toException());
            }
        });
    }
}

activity_read_write.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReadWriteActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:padding="10dp" />

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16dp"
    android:textStyle="bold"/>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vipul Sharma
  • 71
  • 1
  • 8
  • Just confirming, your security rules allow reads only when the user is NOT logged in. Is that the expected behavior ? – Dharmaraj Jun 26 '21 at 17:14
  • Yep, that's correct. The user is not logged in so my security rules allow read only@Dharmaraj – Vipul Sharma Jun 26 '21 at 17:17
  • `auth == null` requires that the user is signed in. Nothing in the code you shared shows that the user is signed in, so I'd expect the `onCancelled` to get called and `Log.w("KEY ERROR", "Failed to read value."` to be logged. – Frank van Puffelen Jun 26 '21 at 18:11

1 Answers1

0

firebaser here

In which region did you create the Realtime Database instance, and when did you download the google-services.json? If it's not the US or if you downloaded the google-services.json before you created the database, the SDK won't be able to determine the correct database URL.

You have two options to fix it in that case:

  1. You can specify the full database URL in the call to getInstance() like this: FirebaseDatabase.getInstance("your database URL").getReference()
  2. You can download an updated google-services.json after you created the database, and make sure you app uses that file.

It's pretty easy to miss this condition right now, as the relevant warning is logged as a debug message. We're working on surfacing the message more explicitly, but in the meantime the above should also work.

Also see: Google Firebase Real-time Database Not Working as everything is set correctly

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hello, I downloaded the google.json file after adding the SDK. To recheck, I removed the older google-services.json and downloaded it again in the app folder. Both the options to fix as per your answer aren't working. I added the original url but it is not returning me the data. Please help, thank you once again! – Vipul Sharma Jun 26 '21 at 17:49