如何根据用户名访问用户数据?

时间:2017-04-30 09:40:09

标签: java android json firebase

我还是java和firebase的新手,我现在正试着玩firebase。

我在Firebase中有这个数据库,我希望能够根据用户名访问子节点的所有数据。

{
  "users" : {
    "lecturers" : {
      "MqTxiHdiwwNZwubExEzGMRG7FN93" : {
        "email" : "c@c.com",
        "password" : "123456",
        "username" : "c"
      }
    },
    "students" : {
      "XPTDRuIGXtZLCfYc8v89Me00NQ22" : {
        "email" : "a@c.com",
        "password" : "123456",
        "username" : "a"
      },
      "Y8DewhUpWUTp7yYjWmp1IOikmhu2" : {
        "email" : "b@c.com",
        "password" : "123456",
        "username" : "b"
      }
    }
  }
}

我想要做的就是,阅读来自用户节点的所有数据,其中包括“学生”或“讲师”这个我尝试过的...(因为我想将用户名值传递给其他功能)

public User uInfo = new User();  //declaring as global variable
public String usrs;   //declaring as global variable

public String goTo(){
  String usr = uInfo.getUsername();
  Toast.makeText(getApplicationContext(), "Test 2: "+ usrs, Toast.LENGTH_SHORT).show();
  return  usr;
}

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

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

  String role = sharedPreferences.getString(ROLE, "");
  Toast.makeText(this, "Role:  " + role, Toast.LENGTH_SHORT).show();
  Log.d("TESTT", "Role:  " + role);

  // Read from the database
  databaseReference.child("users").child(role).addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          FirebaseUser user = mAuth.getCurrentUser();
          String usersID = user.getUid();    
          uInfo.setUsername(dataSnapshot.child(usersID).getValue(User.class).getUsername());

          usrs = uInfo.getUsername();
        //Toast.makeText(getApplicationContext(), "Test 1: "+ usrs, Toast.LENGTH_SHORT).show();
      }
      @Override
      public void onCancelled(DatabaseError error) {
          // Failed to read value
          Log.w(TAG, "Failed to read value.", error.toException());
      }
  });
}

但我所做的代码只访问当前登录应用程序的当前用户的用户名值。

接下来我要做的是比较数据库中用户列表中的用户名并调用其他函数。其中,目前没有用户登录系统。

public void goTo(String username ){
   String usr = uInfo.getUsername(); //Not sure if this correct way 
   String email = uInfo.getEmail();  //Not sure if this correct way 
   String passowrd= uInfo.getPassword();  //Not sure if this correct way 

   if(usr.equals(username)){
       //do something
       //pass(email, password);
   }

}

public void pass(String email, String passwords ){
   //Do something with email and password variable that is passed

}

但我不确定在编码的这一部分我应该做些什么

// Read from the database
  databaseReference.child("users").child(role).addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          //WHat should i change here?
      }
      @Override
      public void onCancelled(DatabaseError error) {
          // Failed to read value
          Log.w(TAG, "Failed to read value.", error.toException());
      }
  });

这是我的User.java

public class User {

    public String username;
    public String email;
    public String password;

    public User(){

    }

    public User(String username, String email, String password) {
        this.username = username;
        this.email = email;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

如果问题太长,我很抱歉。我已经尝试在stackoverflow中搜索解决方案,但我找不到任何问题的答案(或者我的谷歌搜索技能很弱T-T)。你们能帮助我指出正确的方向吗?我已经坚持了这个问题很长一段时间了。

1 个答案:

答案 0 :(得分:0)

Firebase数据库查询直接在您执行该查询的位置下返回子节点。您无法跨多个位置运行单个查询。因此,无法对/users/lecturers/users/students上运行的用户名进行单一查询。

但我觉得你的用户名是独一无二的。在这种情况下,常见的解决方案是在数据库中保留一个将用户名映射到UID的单独节点。

"user_names": {
  "a": "XPTDRuIGXtZLCfYc8v89Me00NQ22",
  "b": "Y8DewhUpWUTp7yYjWmp1IOikmhu2",
  "c": "MqTxiHdiwwNZwubExEzGMRG7FN93"
}

在您的情况下,我会将路径保留给用户的值:

"user_names": {
  "a": "students/XPTDRuIGXtZLCfYc8v89Me00NQ22",
  "b": "students/Y8DewhUpWUTp7yYjWmp1IOikmhu2",
  "c": "lecturers/MqTxiHdiwwNZwubExEzGMRG7FN93"
}

现在你可以从简单的双重读取名称中查找用户:

databaseReference.child("user_names").child(name).addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
      String path = dataSnapshot.getValue(String.class);
      DatabaseReference userRef = databaseReference.child("users").child(path);
      ... now read the user data from userRef
  }