Java 安卓应用赢得';无法写入Firebase实时数据库
我正在使用Firebase和Android Studio构建一个应用程序。我有一个注册屏幕,向用户询问用户名和位置等信息。单击注册按钮时,用户的电子邮件和密码会起作用,并显示在Firebase控制台的“身份验证”选项卡中。但是,我无法获取用户详细信息,如用户名和位置,以存储在Firebase实时数据库中Java 安卓应用赢得';无法写入Firebase实时数据库,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正在使用Firebase和Android Studio构建一个应用程序。我有一个注册屏幕,向用户询问用户名和位置等信息。单击注册按钮时,用户的电子邮件和密码会起作用,并显示在Firebase控制台的“身份验证”选项卡中。但是,我无法获取用户详细信息,如用户名和位置,以存储在Firebase实时数据库中 package com.example.securityapp; import androidx.annotation.NonNull; import androidx.appcompat.a
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);
}
}
将安全规则更改为以下内容:
{
// 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
}
}
将安全规则更改为以下内容:
{
// 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
}
}
使用
mAuth
完成帐户创建后,应将数据位置等存储在Firebase中
//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
// Here we will get the user information, ex userUid (optional)
FirebaseUser user = mAuth.getCurrentUser();
//After that store the data before intent to next activity
insertDataDB();
//Then proceed to next activity
Intent intent_signup = new Intent(register.this, home.class);
startActivity(intent_signup);
finish();
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();
}
// ...
}
});
//代码取自https://firebase.google.com/docs/auth/android/start 2019年11月10日
mAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
//这里我们将获得用户信息,例如userUid(可选)
FirebaseUser=mAuth.getCurrentUser();
//之后,在打算进行下一个活动之前存储数据
insertDataDB();
//然后进行下一个活动
意向\注册=新意向(register.this,home.class);
startActivity(意向登记);
完成();
Toast.makeText(register.this,“身份验证成功!”,Toast.LENGTH_SHORT.show();
}否则{
//如果登录失败,则向用户显示消息。
Toast.makeText(register.this,“身份验证失败”,Toast.LENGTH_SHORT.show();
}
// ...
}
});
更新:
存储方法
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).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
//Success store data in database.
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
}else {
//Something happened
Toast.makeText(getApplicationContext(),"Error: " + task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
public void insertDataDB()
{
字符串username=usernameReg.getText().toString();
字符串office=officeReg.getText().toString();
字符串job=roleReg.getText().toString();
字符串email=emailReg.getText().toString();
String id=databaseUsers.push().getKey();
Users user1=新用户(用户名、办公室、工作、电子邮件);
databaseUsers.child(id).setValue(user1).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//成功将数据存储到数据库中。
Toast.makeText(getApplicationContext(),“Success”,Toast.LENGTH_LONG.show();
}否则{
//出事了
Toast.makeText(getApplicationContext(),“错误:+task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
使用mAuth
完成帐户创建后,应将数据位置等存储在Firebase中
//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
// Here we will get the user information, ex userUid (optional)
FirebaseUser user = mAuth.getCurrentUser();
//After that store the data before intent to next activity
insertDataDB();
//Then proceed to next activity
Intent intent_signup = new Intent(register.this, home.class);
startActivity(intent_signup);
finish();
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();
}
// ...
}
});
//代码取自https://firebase.google.com/docs/auth/android/start 2019年11月10日
mAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
//这里我们将获得用户信息,例如userUid(可选)
FirebaseUser=mAuth.getCurrentUser();
//之后,在打算进行下一个活动之前存储数据
insertDataDB();
//然后进行下一个活动
意向\注册=新意向(register.this,home.class);
startActivity(意向登记);
完成();
Toast.makeText(register.this,“身份验证成功!”,Toast.LENGTH_SHORT.show();
}否则{
//如果登录失败,则向用户显示消息。
Toast.makeText(register.this,“身份验证失败”,Toast.LENGTH_SHORT.show();
}
// ...
}
});
更新:
存储方法
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).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
//Success store data in database.
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
}else {
//Something happened
Toast.makeText(getApplicationContext(),"Error: " + task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
public void insertDataDB()
{
字符串username=usernameReg.getText().toString();
字符串office=officeReg.getText().toString();
字符串job=roleReg.getText().toString();
字符串email=emailReg.getText().toString();
String id=databaseUsers.push().getKey();
Users user1=新用户(用户名、办公室、工作、电子邮件);
databaseUsers.child(id).setValue(user1).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//成功将数据存储到数据库中。
Toast.makeText(getApplicationContext(),“Success”,Toast.LENGTH_LONG.show();
}否则{
//出事了
Toast.makeText(getApplicationContext(),“错误:+task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
这是我收到的通知,但我没有使用Cloud Firestore,我正在尝试使用实时数据库
这是我收到的通知,但我没有使用Cloud Firestore,我正在尝试使用实时数据库谢谢您的回复,但我已经设置好了这些,仍然没有运气:(方法
insertDataIntoDb
应该在createuserwithEmailAndPassword
中,我也尝试将insertDataDB方法移到这里,但仍然没有:(我真的很困惑到底发生了什么。一位朋友的代码与我的代码完全相同,而她的代码似乎有效?谢谢你的回复,但我已经设置好了这些代码,仍然没有运气:(方法insertDataIntoDb
应该在createuserwithEmailAndPassword
中,我也尝试过将insertDataDB方法移到这里,但仍然没有任何结果:(我真的很困惑到底发生了什么。一个朋友的代码和我的完全一样,她的似乎可以工作?谢谢你的回复。当我移动insertData()时);方法之前的意图,我仍然得到相同的问题