[英]Why is my java code not sending data to firebase?
So I'm building an Android app that adds users to the Firebase database from a form inside the Register Activity. 因此,我正在构建一个Android应用程序,该应用程序通过Register Activity内的表单将用户添加到Firebase数据库。
I'm building this in Android studio 3 and already ran it on my HTC One M9 我正在Android Studio 3中构建它,并且已经在我的HTC One M9上运行了它
package com.example.mobilniproekt;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
import java.util.Map;
public class RegisterActivity extends AppCompatActivity {
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button button=(Button) findViewById(R.id.register);
databaseReference= FirebaseDatabase.getInstance().getReference("users");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText name=(EditText) findViewById(R.id.name);
EditText lastname=(EditText) findViewById(R.id.lastname);
EditText city=(EditText) findViewById(R.id.city);
EditText email=(EditText) findViewById(R.id.email);
EditText username=(EditText) findViewById(R.id.username);
EditText password=(EditText) findViewById(R.id.password);
String id=databaseReference.push().getKey();
User user=new User(id, name.getText().toString(), lastname.getText().toString(), city.getText().toString(), email.getText().toString(), username.getText().toString(), password.getText().toString());
databaseReference.child(id).setValue(user);
name.setText("");
}
});
}
}
However when I launch the app, fill out the form and check my database there isn't any data. 但是,当我启动该应用程序时,请填写表格并检查我的数据库中没有任何数据。
Am I implementing the code wrong or is there another problem with the Firebase configuration? 我实施的代码是否错误或Firebase配置是否还有其他问题?
You need to create a user first for them 您需要先为他们创建一个用户
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
} else {
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
Or if they already have account, they need to sign in first with their details 或者,如果他们已经拥有帐户,则需要先使用其详细信息登录
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
}
else {
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
Then you can send to firebase whatever data to write 然后,您可以将要写入的任何数据发送到Firebase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.