gpt4 book ai didi

java - Android 应用程序不会写入 Firebase 实时数据库

转载 作者:行者123 更新时间:2023-11-30 04:53:49 25 4
gpt4 key购买 nike

我正在使用 Firebase 和 Android Studio 构建应用。我有一个注册屏幕,要求用户提供用户名和位置等信息。单击注册按钮后,用户的电子邮件和密码将起作用并显示在 Firebase 控制台的身份验证选项卡中。但是,我无法获取用户名和位置等用户详细信息以存储在 Firebase 实时数据库中。

package com.example.securityapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class register extends AppCompatActivity {

private FirebaseAuth mAuth;
EditText emailReg, passwordReg, password2, roleReg, officeReg;
Button regButton;
DatabaseReference databaseUsers;
EditText usernameReg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

emailReg = (EditText) findViewById(R.id.regEmail);
usernameReg = (EditText) findViewById(R.id.regUsername);
passwordReg = (EditText) findViewById(R.id.regPassword);
regButton = (Button) findViewById(R.id.register_Button);
password2 = (EditText) findViewById(R.id.confirm_password);
roleReg = (EditText) findViewById(R.id.regRole);
officeReg = (EditText) findViewById(R.id.regOffice);
databaseUsers = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
}

public void registerUser() {
final String email = emailReg.getText().toString();
final String password = passwordReg.getText().toString();
String pass2 = password2.getText().toString();
if(!pass2.equals(password))
{
Toast.makeText(register.this, "Passwords Do Not Match",
Toast.LENGTH_SHORT).show();
return;
}
//Code taken from https://firebase.google.com/docs/auth/android/start on 10/11/2019
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, 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

Intent intent_signup = new Intent(register.this, home.class);
startActivity(intent_signup);
finish();
FirebaseUser user = mAuth.getCurrentUser();
Toast.makeText(register.this, "Authentication Successful!",
Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(register.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// ...
}
});

}


public void registerOnClick(View view)
{
regButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{

registerUser();
insertDataDB();

}
});


}

public void insertDataDB()
{
String username = usernameReg.getText().toString();
String office = officeReg.getText().toString();
String job = roleReg.getText().toString();
String email = emailReg.getText().toString();


String id = databaseUsers.push().getKey();

Users user1 = new Users(username, office, job, email);
databaseUsers.child(id).setValue(user1);


}

}

用户.java

package com.example.securityapp;

public class Users {

String username;
String job;
String email;
String office;

public Users()
{

}

public Users(String username, String job, String email, String office)
{
this.username = username;
this.job = job;
this.email = email;
this.office = office;
}

public String getUsername()
{
return username;
}

public String getJob() { return job; }

public String getEmail()
{
return email;
}

public String getOffice()
{
return office;
}



}

这是我收到的通知,但我没有使用 Cloud Firestore,我正在尝试使用实时数据库

最佳答案

将安全规则更改为以下内容:

{
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this ruleset in production; it allows
// anyone to overwrite your entire database.

"rules": {
".read": true
".write": true
}
}

关于java - Android 应用程序不会写入 Firebase 实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59569425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com