I am trying to read data from Firebase but it is not read by android studio although I am using the tutorial
I tried to copy the link that is sent by Android Studio to Firebase:
DatabaseReference myRef = database.getReference("USERS").child(uID).child("DeviceID");
textview.setText(myRef.toString());
and past the result in the browser and it shows me the result in firebase but when I try to use it to get data it is not retrieving anything.
here is how I am trying to read it by calling a function:
textview.setText(ReadDeviceId);
''''''''''''''''''''''''''''''''''''
private String ReadDeviceId(){
FBUser = FirebaseAuth.getInstance().getCurrentUser();
uID = FBUser.getUid();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("USERS").child(uID).child("DeviceID");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
r_deviceID = dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
r_deviceID = "no userID";
}
});
return r_deviceID;
}
'''''''''''''''''''''''''''''''''''''''''''
Knowing that my firebase database security rule is:
'''''''''''''''''''''''''''''''''
{
"rules": {
".write": "auth != null",
".read": true
}
}
'''''''''''''''''''
but nothing is displayed
HussainAbbas :
you can try this code \n\n private String ReadDeviceId(){\n FBUser = FirebaseAuth.getInstance().getCurrentUser();\n uID = FBUser.getUid();\n FirebaseDatabase database = FirebaseDatabase.getInstance();\n DatabaseReference myRef = \n database.getReference(\"USERS\").child(uID).child(\"DeviceID\");\n\n myRef.addValueEventListener(new ValueEventListener() {\n @Override\n public void onDataChange(DataSnapshot dataSnapshot) {\n for(Datasnapshot snapshot : dataSnapshot.getChildren)\n {\n //try to map it with your on model class and replace the String with your model class\n r_deviceID = snapshot.getValue(String.class)\n }\n //r_deviceID = dataSnapshot.getValue(String.class);\n }\n\n @Override\n public void onCancelled(DatabaseError error) {\n // Failed to read value\n r_deviceID = \"no userID\";\n }\n });\n return r_deviceID;\n }\n",
2019-04-08T12:39:02