如何将名称以及电子邮件和密码添加到firebase数据库android

时间:2017-09-28 12:13:38

标签: java android firebase firebase-realtime-database

如何将姓名以及电子邮件和密码添加到firebase数据库android?

我添加了一个功能createUserWithEmailAndPassword的电子邮件和密码,并调用了一个函数:

createNewUser(task.getResult().getUser());

成功时和在createnewUser中执行此操作

private void createNewUser(FirebaseUser userFromRegistration) {
        String username = nameEditText.getText().toString();
        String email = userFromRegistration.getEmail();
        String userId = userFromRegistration.getUid();


        User user = new User();
        user.setName(username);
        user.setUid(userId);
        user.setEmail(email);



        Log.d("Raza",mDatabase.child("users").push().setValue(user).isSuccessful()+"");

    }

但我在Log.d收到False并且用户名未添加到我的数据库中。

我的用户类是:

public class User {


String name;
String uid;
String email;

public User(){
    // Default constructor required
}

public void setName(String name){this.name = name;}
public void setUid(String uid){this.uid = uid;}
public void setEmail(String email){this.email = email;}

public String getName(){return this.name;}



}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

模型类

public class User {
public String uid;
public String email;
public String user_name;
 public User(){

}
public User(String uid, String email String user_name)
{
    this.uid = uid;
    this.email = email;
    this.user_name=user_name;
}
}

并将数据推送到像

这样的firebase数据中
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
    User user = new User(firebaseUser.getUid(),
            firebaseUser.getEmail(),
            MySharedPreferences.getString(Constants.KEY_USER_NAME));
            database.child(Constants.ARG_USERS)
            .child(firebaseUser.getUid())
            .setValue(user)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
//  mOnUserDatabaseListener.onSuccess(context.getString(R.string.user_successfully_added));
                      } else {
//   mOnUserDatabaseListener.onFailure(context.getString(R.string.user_unable_to_add));
                    }
                }
            });