我需要从我的Firebase数据库中的节点密码中获取字符串值以与用户输入进行比较,但不幸的是我无法获取该值。这是下面图片中我的Firebase数据库的链接。从Firebase数据库获取价值
这是我下面的代码:
final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("pin_code");
mDatabase.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
String rface = (String) dataSnapshot.child("pincode").getValue();
if (rface.equals(userPassword) && !rface.equals("")){
Intent intent = new Intent(PinActivity.this, ProfileActivity.class);
startActivity(intent);
}
else {
if (rface.equals("") || rface.equals(null)){
// Creating new user node, which returns the unique key value
// new user node would be /users/$userid/
String userId = mDatabase.push().getKey();
// creating user object
Pin pin = new Pin(authUserId, userPassword);
mDatabase.child(userId).setValue(pin);
Intent intent = new Intent(PinActivity.this, ProfileActivity.class);
startActivity(intent);
}
else {
Toast.makeText(PinActivity.this,"Invalid PIN code", Toast.LENGTH_SHORT).show();
return;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是JSON代码
{
"pin_code" : {
"id" : "TQYTo1NHNnhPJnOxhe1Vok3U6ic2",
"pincode" : "12345"
}
}
我编辑了我的代码与您的建议,但现在它不返回给我。 –
我在我的最初片段中发生了错误。我只是解决了这个问题,所以看看能否解决你现在遇到的问题。 –
我用第二种方法,它不返回任何值 –