无法注册用户firebase:发生了网络错误(例如超时,中断连接或无法访问的主机)?

时间:2018-02-19 12:16:13

标签: java android firebase firebase-authentication

我正在尝试使用Firebase电子邮件和密码身份验证注册用户。我以前通过跟踪和阅读多个教程来完成它。但是,它现在根本没有注册。我已经检查了所有gradle文件和权限,似乎没有任何遗漏。

它会自动转到else语句并输出Toast消息。这是行

else
{
    progressDialog.dismiss();
    Toast.makeText(RegisterActivity.this, "Could Not Register. Please Check Your Details Again And Try Again", Toast.LENGTH_SHORT).show();
}

我看不出有什么变化让它失败了。它们是一个格式正确的电子邮件和密码,也可以发送到createUserWithEmailAndPassword方法。

private void registerUser() {
        final String email = editTextEmail.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();

        // email is too short
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(this, "Please Enter An Email Address", Toast.LENGTH_SHORT).show();
            return;
        }

        // password to short
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please Enter A Password", Toast.LENGTH_SHORT).show();
            return;
        }

        if (password.length() < 6) {
            Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters", Toast.LENGTH_SHORT);
        }

        if (checkBox.isChecked()) {
            //if validation is ok, show progress bar
            progressDialog.setMessage("Registering User Please Wait");
            progressDialog.show();

            firebaseAuth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                progressDialog.dismiss();
                                Toast.makeText(RegisterActivity.this, "Registered Sucessfully", Toast.LENGTH_SHORT).show();
                                finish();
                                startActivity(new Intent(getApplicationContext(), CreateProfileActivity.class));
                            } else if (!task.isSuccessful() && password.length() < 6){
                                progressDialog.dismiss();
                                Toast.makeText(RegisterActivity.this, "Could Not Register. Passwords much be at least 6 characters ", Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                                progressDialog.dismiss();
                                Toast.makeText(RegisterActivity.this, "Could Not Register. Please Check Your Details Again And Try Again", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
    }

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

        firebaseAuth = FirebaseAuth.getInstance();

        if(firebaseAuth.getCurrentUser() != null)
        {
            finish();
            startActivity(new Intent(getApplicationContext(), CreateProfileActivity.class));
        }

        progressDialog = new ProgressDialog(this);
        buttonRegister = (Button) findViewById(R.id.buttonRegister);
        editTextEmail = (EditText) findViewById(R.id.editTextEmailLogin);
        editTextPassword = (EditText) findViewById(R.id.editTextPasswordLogin);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        textViewSignUp = (TextView) findViewById(R.id.textViewSignIn);

        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUser();
            }
        });

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

1 个答案:

答案 0 :(得分:1)

改变这个:

Toast.makeText(RegisterActivity.this, "Could Not Register. Please Check Your Details Again And Try Again", Toast.LENGTH_SHORT).show();

进入这个:

Toast.makeText(RegisterActivity.this, "User Authentication Failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();

知道问题可能是什么。

根据错误,你需要检查谷歌播放服务是否正在运行,建议在物理设备上测试应用程序而不是模拟器,或者在模拟器上下载谷歌播放服务,因为并非所有人都拥有它。 / p>