我有这个应用程序,用于注册患者的个人信息,并将其作为Json格式存储在数据库中,格式如下:
Patients {
patient id e.g.123456789 {
name:
email:
password:
bloodPressureReading {
10:37:13AM29Oct-2019:{
dia:34
prp:45
sys:12
}
}
}
}
我正在登录页面上,我需要用户输入用户名和密码来完成简单的登录过程。但是,我不知道如何从Firebase实时数据库中检索密码值。
P.S。我不想使用Firebase身份验证服务
我尝试了这段代码,但是没有用:
databasePatients.child("\"Patients\"").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//showData(dataSnapshot);
//old Code
/*
for(DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()){
//Loop 1 to go through all the child nodes of users
for(DataSnapshot booksSnapshot : uniqueKeySnapshot.child("123456789").getChildren()){
//loop 2 to go through all the child nodes of books node
//String bookskey = booksSnapshot.getKey();
//String password = booksSnapshot.getValue();
}
}
}*/
我也尝试过此代码,尽管ID和密码正确,它也不会返回任何结果
按钮testbutton =(按钮)findViewById(R.id.button);
// To move to register page
testbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty( (testid.getText().toString().trim()))) {
Toast.makeText(getApplicationContext(), "Enter id to search for!", Toast.LENGTH_SHORT).show();
return;
}else
{
Query query = databasePatients.child("Patients").orderByChild("id").equalTo(testid.getText().toString().trim());
//Query query = databasePatients.child("Patients").child("123456789").orderByChild("id").equalTo(testid.getText().toString().trim());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Context context = getContext();
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot user : dataSnapshot.getChildren()) {
// do something with the individual "issues"
patients usersBean = user.getValue(patients.class);
if (usersBean.password.equals(txvPassword.getText().toString().trim())) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Password is wrong", Toast.LENGTH_LONG).show();
}
}
} else {
Toast.makeText(getApplicationContext(), "User not found", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
我也在 Login using email stored in firebase realtime database ,但我不知道为什么(dataSnapshot.exists())返回false。
任何帮助将不胜感激。