gpt4 book ai didi

java - 注册表未写入数据库

转载 作者:行者123 更新时间:2023-12-01 17:15:02 25 4
gpt4 key购买 nike

我正在尝试制作注册表单,并且想要保存用户输入的数据,但是当我运行应用程序并检查 Firebase 上的数据库时,什么也没有,我已经确保我的数据库已连接到此应用程序,但我仍然没有得到任何东西,这是我到目前为止所得到的:

package com.example.parkingapp;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

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.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


public class RegisterActivity extends AppCompatActivity {
private static final String TAG = "RegisterPage";

private EditText fName, lName, userEmail, userPassword, pNumber,schoolName;
private Button registerBtn;
private FirebaseAuth firebaseAuth;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myReference = database.getReference("https://authenticate-user-443f2.firebaseio.com/");

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

fName = (EditText) findViewById(R.id.first_name);
lName = (EditText) findViewById(R.id.last_name);
userEmail = (EditText) findViewById(R.id.email_address);
userPassword = (EditText) findViewById(R.id.password);
pNumber = (EditText) findViewById(R.id.phone_number);
schoolName = (EditText) findViewById(R.id.school_name);
registerBtn = (Button) findViewById(R.id.register_user);

firebaseAuth = FirebaseAuth.getInstance();

//registerBtn.setOnClickListener(new View.OnClickListener() {
// @Override
//public void onClick(View v) {
// RegisterActivity.this.startActivity(new Intent(RegisterActivity.this, RegisterActivity.class));
//}
//});

registerBtn.setOnClickListener((View.OnClickListener) this);
}

// registerBtn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// RegisterActivity.this.finish();
//}
//});

//registerBtn.setOnClickListener(new View.OnClickListener() {
// @Override
//public void onClick(View v) {
// RegisterActivity.this.registerUser();
//}
//});

//}


public void onClick(View view){
if(view == registerBtn) {
registerUser();
}
}

private void registerUser() {
final String firstName = fName.getText().toString();
final String lastName = lName.getText().toString();
final String email = userEmail.getText().toString();
final String password = userPassword.getText().toString();
final String phoneNumber = pNumber.getText().toString();
final String school = schoolName.getText().toString();


if (TextUtils.isEmpty(email)) {
showToast("Enter email address!");
return;
}

if(TextUtils.isEmpty(password)){
showToast("Enter Password!");
return;
}

if(userPassword.length() < 6){
showToast("Password too short, enter minimum 6 characters");
return;
}


//register user
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "New user registration: " + task.isSuccessful());

if (task.isSuccessful()) {
// RegisterActivity.this.showToast("Authentication failed. " + task.getException());
DatabaseReference ref = database.getReference("https://authenticate-user-443f2.firebaseio.com/");
DatabaseReference firstN = ref.child("first name");
firstN.setValue(firstName);
DatabaseReference lastN = ref.child("last name");
lastN.setValue(lastName);
DatabaseReference emailAddress = ref.child("email");
emailAddress.setValue(email);
DatabaseReference password = ref.child("password");
password.setValue(password);
DatabaseReference phone = ref.child("phone number");
phone.setValue(phoneNumber);
DatabaseReference schoolName = ref.child("school");
schoolName.setValue(school);


} else {
RegisterActivity.this.startActivity(new Intent(RegisterActivity.this, DestinationActivity.class));
RegisterActivity.this.finish();
}
}
});
}

@Override
protected void onResume() {
super.onResume();

}

public void showToast(String toastText) {
Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
}

}

感谢任何帮助!

这是日志猫: enter image description here

最佳答案

您应该使用 getReferenceUrl :

Gets a DatabaseReference for the provided URL.

DatabaseReference ref = database.getReferenceUrl("https://authenticate-user-443f2.firebaseio.com/");

Gets a DatabaseReference for the database root node.

DatabaseReference ref = database.getReference();
<小时/>

那么你想给哪个用户设置值?(未知)你必须设置一个用户id(唯一id)。

ref.child("users").child("user_id").child("first name").setValue(firstName);

我认为你应该创建用户对象。这对你来说会更有用。

关于java - 注册表未写入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61403695/

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