我想使用Firebase实时数据库中存储的用户名和密码登录。但是我的代码不起作用。我包括我的数据库图片和代码。请帮助我。
这是数据库映像:
这是我的主要活动代码MainActivity.java
databaseReference=FirebaseDatabase.getInstance().getReference("Client");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Query query = databaseReference.child("Client").orderByChild("username").equalTo(editTextUsername.getText().toString().trim());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot user : dataSnapshot.getChildren()) {
Users usersBean = user.getValue(Users.class);
if (usersBean.password.equals(editTextPassword.getText().toString().trim())) {
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
这是我的用户java类Users.java
public class Users {
String email;
String id;
String password;
String username;
public Users() {
}
public Users(String email, String id, String password, String username) {
this.email = email;
this.id = id;
this.password = password;
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
答案 0 :(得分:2)
要解决此问题,请更改以下代码行:
databaseReference=FirebaseDatabase.getInstance().getReference("Client");
到
databaseReference=FirebaseDatabase.getInstance().getReference();
由于您在查询中使用对Client
的调用,因此无需获取.child("Client")
节点的引用。
编辑:
要解决您根据注释获得的错误,请将id
类中Users
属性的类型从String
更改为long
,然后将获取方法更改为和这样的二传手:
public class Users {
String email;
long id;
String password;
String username;
public Users() {
}
public Users(String email, long id, String password, String username) {
this.email = email;
this.id = id;
this.password = password;
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}