Why i can't retrieve username and password from Firebase realtime database

牧云@^-^@ 提交于 2021-01-28 02:15:29

问题


I want to login using username and password that is stored in Firebase realtime database. But my code does not work. I include my database picture and code. Please help me.

This is database image:

This is my main activity code 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) {

                }
            });
        }
    });

Here is my users java class 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;
    }
}

回答1:


To solve this, please change the following line of code:

databaseReference=FirebaseDatabase.getInstance().getReference("Client");

to

databaseReference=FirebaseDatabase.getInstance().getReference();

There is no need to get a reference of the Client node since you are using a call to .child("Client") in your query.

Edit:

To solve the error you get now according to your comment, plase change the type of your id property in your Users class from String to long and the getter and setter like this:

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;
    }
}


来源:https://stackoverflow.com/questions/55334387/why-i-cant-retrieve-username-and-password-from-firebase-realtime-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!