我正在将用户输入Firebase,例如:
private void Database(String mail, String password){
auth.createUserWithEmailAndPassword(mail, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("CreateUser", "createUserWithEmail:success");
showToast("User successfully created");
insertUserData();
} else {
// If sign in fails, display a message to the user.
Log.w("CreateUser", "createUserWithEmail:failure", task.getException());
showToast("Authentication failed. User exist!");
}
}
});
}
现在,我想添加其他将在调用 insertUserData()
的用户上链接的用户信息。我创建了类似于以下内容的“ User”类:
public class User {
private String name;
private String surname;
private String phone;
private String address;
private String country;
private String zip;
public User(){
}
public User (String mail, String surname, String phone, String address, String country, String zip){
this.name = name;
this.surname = surname;
this.phone = phone;
this.address = address;
this.country = country;
this.zip = zip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//...
//...
我的问题是我应该如何在链接到Users的firebase中插入其他用户数据,我试图在firebase中创建一个新数据库,如下所示:
在Android Studio中:
private void insertUserData(){
FirebaseUser username = auth.getCurrentUser();
String userId = username.getUid();
Firebase rootRef = new Firebase(MYURL);
Firebase userRef = rootRef.child("Users");
User user = new User();
user.setName("Test");
user.setSurname("Test");
userRef.child(userId).setValue(user);
}
但是我不确定 MYURL 的含义,听说它是用于旧的Firebase,我应该换成新的东西。
更新:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:16.0.6'
implementation 'com.google.firebase:firebase-firestore:21.3.1'
}
答案 0 :(得分:2)
以下代码将根据FirebaseAuth
提供的uid设置用户
private void insertUserData(){
FirebaseUser user = auth.getCurrentUser();
String uid = user.getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();
Map<String, Object> user = new HashMap<>();
user.put("first", "Ada");
user.put("last", "Lovelace");
user.put("born", 1815);
// Add a new document with a generated ID
db.collection("users")
.document(uid)
.set(user);
}
仅使用Firestore:
dependencies {
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-firestore:21.3.1'
}
答案 1 :(得分:1)
您使用的代码用于实时数据库,但问题中的图片用于Firestore。如果要使用Firestore,则必须执行以下操作:
Map<String, Object> userData = new HashMap<>();
userData.put("name", "john");
userData.put("address", "address");
userData.put("country", "USA");
db.collection("User").document("User")
.set(userData)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});
在此处查看更多信息:
https://firebase.google.com/docs/firestore/manage-data/add-data
答案 2 :(得分:1)
如果您已将google-services.json文件复制到您的项目中,则不必担心 MYURL ,我已经修改了您的 insertUserData 函数,该函数将用户数据存储到Firestore数据库
private void insertUserData(){
FirebaseUser username = FirebaseAuth.getInstance().getCurrentUser();
String userId = username.getUid();
User user = new User();
user.setName("Test");
user.setSurname("Test");
FirebaseFirestore.getInstance()
.collection("users")
.document(userId)
.set(user, SetOptions.merge());
}
如果您不希望清除现有数据,请使用选项 SetOptions.merge()