在用户登录/登录个人资料后,我正在尝试在firebase中创建一个实时数据库。我进行了身份验证firebase,这表明我处于firebase身份验证中。我尝试了很多教程但没有任何效果。如何连接身份验证和实时数据库以创建用户个人资料。
规则:
"rules": {
".read": "auth != null",
"$user_id":{
".read": "auth.uid === $user_id",
".write": "auth.uid === $user_id"
}
}
}
用户类别:
public String firstName;
public String secondName;
public String uid;
public String email;
public User(){
}
public User (String firstName, String secondName, String uid, String email) {
this.firstName = firstName;
this.secondName = secondName;
this.uid=uid;
this.email=email;
}
public User (String firstname, String secondname) {
}
public String getFirstName () {
return firstName;
}
public String getSecondName () {
return secondName;
}
public String getUid () {
return uid;
}
public String getEmail () {
return email;
}
public void setFirstName (String firstName) {
this.firstName = firstName;
}
public void setSecondName (String secondName) {
this.secondName = secondName;
}
public void setUid (String uid) {
this.uid = uid;
}
public void setEmail (String email) {
this.email = email;
}
}
和UserDetails类:
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class UserDetails extends AppCompatActivity {
private FirebaseAuth auth;
private DatabaseReference reference;
private FirebaseDatabase database;
private FirebaseAuth.AuthStateListener authStateListener;
private FirebaseUser user;
private EditText firstName,secondName;
private Button saveInfo;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_user_details );
firstName=(EditText)findViewById ( R.id.firstname );
secondName=(EditText)findViewById ( R.id.secondname );
saveInfo=(Button)findViewById ( R.id.data );
auth=FirebaseAuth.getInstance ();
reference.addValueEventListener ( new ValueEventListener () {
@Override
public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
String firstname=dataSnapshot.getValue (User.class)
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError) {
}
} )
}
}
我在DataSnapshot上删除了一些代码,因为它不起作用。该怎么办?我使用了很多教程,但是数据库中没有任何内容。我想在数据库中写入用户名,名字和名字
答案 0 :(得分:0)
在您询问编写时,代码reference.addValueEventListener
从数据库中读取。
要写入数据库,您可以执行以下操作:
User user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference(user.getUid());
userRef.child("firstname").setValue("Value of firstname field");
userRef.child("secondname").setValue("Value of secondname field");
}
这将创建一个结构,将用户数据存储在其UID下,这是您的安全规则所强制执行的。
您可以在单个setValue()
调用中将两个写操作合并为:
Map<String,Object> values = new HashMap<>();
values.put("firstname", "Value of firstname field");
values.put("secondname", "Value of secondname field");
userRef.setValue(values);
如果您不想使用值映射,则可以创建一个Java类来表示用户的属性。具有这两个JSON属性的类的最简单版本是:
class UserProperties {
public string firstname;
public string secondname;
}
然后初始化并使用以下代码编写:
UserProperties userprops = new UserProperties();
userprops.firstname = "Value of firstname field";
userprops.secondname = "Value of secondname field";
userRef.setValue(userprops);