【问题标题】:Firebase Realtime Database ConfiguringFirebase 实时数据库配置
【发布时间】:2019-05-19 11:24:08
【问题描述】:

在用户登录/登录他的个人资料后,我正在尝试在 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 中删除了一些代码,因为它不起作用。我该怎么办?我使用了很多教程,但没有在数据库中写入任何内容。我想在数据库中写入用户 ID、名字和第二个名字

【问题讨论】:

    标签: android firebase-realtime-database


    【解决方案1】:

    代码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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 2021-03-01
      • 2019-01-18
      • 1970-01-01
      • 2021-12-12
      • 2020-07-05
      • 2021-07-01
      相关资源
      最近更新 更多