1

I need read data from my Firebase database.I will try my best but the addValueListener not fire.

DatabaseReference referenceParty = database.getReference();
referenceParty.child("Party").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            PartyInf info = snapshot.getValue(PartyInf.class);
            String acCode = info.getAC_Code();
            String acName = info.getAC_Name();
            String arCode = info.getArea_Code();
            PartyExist = dbMain.hasObjectParty(Integer.parseInt(acCode));
            if (PartyExist == false && !acName.trim().equals("")) {
               dbMain.addParty(Integer.parseInt(acCode), acName, Integer.parseInt(arCode));
            }
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    }
 });
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
tsrahul81
  • 21
  • 2
  • The first step is to implement `onCancelled` so that you don't ignore errors. At its minimum that should be `public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }`. --- Then check if either `onDataChange` or `onCancelled` is called by running in a debugger. If neither is called, you might not have configured your database URL correctly, and you can pass it into the API call with: `DatabaseReference referenceParty = FirebaseDatabase.getInstance("database URL here").getReference();` For mre on this, see https://stackoverflow.com/q/68806876 – Frank van Puffelen Oct 19 '22 at 11:17
  • Make sure your Firebase database path is correct and you are writing data to the correct path. – Lawrence Gimenez Oct 19 '22 at 11:22
  • I will try this but still no result.Neither onDataChenge or onCancelled not called FirebaseDatabase database = FirebaseDatabase.getInstance("https://easypay-default-rtdb.firebaseio.com"); DatabaseReference referenceArea = database.getReference("Area"); – tsrahul81 Oct 19 '22 at 11:53
  • @LawrenceGimenez that i checked . It's correct – tsrahul81 Oct 19 '22 at 11:54
  • On your question, you are writing to Party? – Lawrence Gimenez Oct 19 '22 at 11:56
  • @LawrenceGimenez Area & Party exist.Both not working – tsrahul81 Oct 19 '22 at 12:03
  • Can you make sure your google-services.json is correct? And review your database rules on Firebase admin. – Lawrence Gimenez Oct 19 '22 at 12:06
  • @LawrenceGimenez "google-services.json" i already checked it's correct. my database rule is { "rules": { ".read": true, ".write": true } } – tsrahul81 Oct 20 '22 at 06:02
  • @LawrenceGimenez It's not code side error.It's emulator's based bug.I turn off emulator wifi.Then it's work correctly. – tsrahul81 Oct 22 '22 at 09:35
  • @tsrahul81 thanks for the update! Yes always test on a device. I don't trust Android emulators. – Lawrence Gimenez Oct 22 '22 at 09:36

1 Answers1

1

It was not a problem with my code. It turned out to be an emulator bug. I turned off the emulator's wifi, then it works correctly.

Pandapip1
  • 730
  • 5
  • 15
tsrahul81
  • 21
  • 2