Android Studio将数据发送到Firebase,但Firebase数据库中没有保存数据

时间:2018-04-29 23:58:39

标签: java android firebase firebase-realtime-database

与Firebase的连接成功,我确实拥有所有必要的导入。这段代码中是否存在逻辑错误?

数据将发送到Firebase,但永远不会存储到数据库中。我的数据库命名是正确的。它如下 - > 数据库名称 - > 2children(客户)(Riders)。拼写已被仔细检查过。

public class DriverLoginActivity extends AppCompatActivity {
        private EditText mEmail, mPassword;
        private Button mLogin, mRegistration;

        private FirebaseAuth mAuth;
        private FirebaseAuth.AuthStateListener firebaseAuthListener;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_driver_login);

            mAuth = FirebaseAuth.getInstance();

            firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    if(user!=null){
                        Intent intent = new Intent(DriverLoginActivity.this, MapActivity.class);
                        startActivity(intent);
                        finish();
                        return;
                    }
                }
            };

            mEmail = (EditText) findViewById(R.id.email);
            mPassword = (EditText) findViewById(R.id.password);

            mLogin = (Button) findViewById(R.id.login);
            mRegistration = (Button) findViewById(R.id.registration);

            mRegistration.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String email = mEmail.getText().toString();
                    final String password = mPassword.getText().toString();
                    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(DriverLoginActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(!task.isSuccessful()){
                                Toast.makeText(DriverLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
                            }else{
                                String user_id = mAuth.getCurrentUser().getUid();
                                DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(user_id);
                                current_user_db.setValue(true);
                            }
                        }
                    });
                }
            });

            mLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String email = mEmail.getText().toString();
                    final String password = mPassword.getText().toString();
                    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(DriverLoginActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(!task.isSuccessful()){
                                Toast.makeText(DriverLoginActivity.this, "sign in error", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }
            });
        }


        @Override
        protected void onStart() {
            super.onStart();
            mAuth.addAuthStateListener(firebaseAuthListener);
        }
        @Override
        protected void onStop() {
            super.onStop();
            mAuth.removeAuthStateListener(firebaseAuthListener);
        }
    }




  [1]: https://i.stack.imgur.com/xMWqS.jpg

1 个答案:

答案 0 :(得分:0)

代码似乎没问题。用户注册成功吗?如果是,请检查数据库规则。如果不是,请检查您的注册码。