Firebase身份验证-发生网络错误(例如超时,连接中断或主机无法访问)

时间:2019-03-03 17:23:33

标签: android firebase firebase-authentication

更新

我的应用程序不断在实时应用程序中崩溃。每周有100-200次崩溃。所以我开始悬赏这个问题。如果有人解决了。请帮忙。

但是,有99%的用户没有崩溃。这些崩溃可能会影响我的应用在Play商店上的存储库。因此,如果我没有解决方案,那么我将最终删除此功能,以在FirebaseAuth中通过电子邮件/密码登录:/

问题:

我在Android应用程序Firebase Auth Login中发生了多次崩溃(来自249个用户的295次崩溃)。尝试通过电子邮件密码登录的1-2%用户遇到此崩溃。我研究了此错误,并得到了提示,当this answer.不能运行播放服务时会发生这种情况

仅供参考,google / facebook身份验证运行正常。此问题仅在通过电子邮件密码登录中。

我尝试了什么?

我试图在登录方法上放置一个try-catch块。并尝试捕获此异常。作为解决方案,我想显示一个对话框,通知用户播放服务未运行。而且他可以手动打开Play商店以启动播放服务,然后再访问此页面。

但是,正如我在Fabric上看到的那样,崩溃永远不会引起阻塞,并且当发生此异常时应用程序崩溃。因为FirebaseAuth SDK中会引发此错误。

需要什么?

首先,我想确定此异常的原因。如果由于播放服务未运行而发生这种情况,那么我想向用户显示一个对话框。当前从不显示,并且在发生以下异常后崩溃。

Fatal Exception: com.google.android.gms.g.f: com.google.firebase.e: A network error (such as timeout, interrupted connection or unreachable host) has occurred.
       at com.google.android.gms.tasks.zzu.getResult(Unknown Source)
       at com.startech.dreamteam11.app.activities.ActivityLogin.tryLogin(Unknown Source)
       at com.startech.dreamteam11.app.activities.ActivityLogin.lambda$-wlX6lv_j3Q0nUN9OuqzHS7ZGP4(Unknown Source)
       at com.startech.dreamteam11.app.activities.-$$Lambda$ActivityLogin$-wlX6lv_j3Q0nUN9OuqzHS7ZGP4.onComplete(lambda)
       at com.google.android.gms.tasks.zzj.run(Unknown Source)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6946)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

代码

    private void loginViaEmailPassword(String email, String pass) {
        showProgressBar();
        try {
             // check if user is registered. then try login
            FirebaseAuth.getInstance().fetchSignInMethodsForEmail(email).addOnCompleteListener(task -> {
                SignInMethodQueryResult result = task.getResult();
                if (task.isSuccessful() && result != null && result.getSignInMethods() != null && result.getSignInMethods().size() > 0) {
                    // user is registered, now try login
                    tryLogIn(email, pass, new OnFireBaseLogin() {
                        @Override
                        public void onSuccess(FirebaseUser user) {
                            // check if email is verified, if not send verification email.
                            if (user.isEmailVerified()) {
                                // user is verified, redirect to main screen
                                startMainActivity();
                            } else {
                                sendVerificationEmail(user, task1 -> {
                                    hideProgressBar();
                                    FirebaseAuth.getInstance().signOut();
                                    if (task1.isSuccessful()) {
                                        Utilities.getInstance().showDialog(ActivityLogin.this, getString(R.string.sent_verification_email), getString(R.string.msg_sent_verification_email), (dialog, which) -> {
                                            dialog.dismiss();
                                        });
                                    } else {
                                        errorMessage(getString(R.string.msg_error_sending_email));
                                    }
                                });
                            }
                        }

                        @Override
                        public void onError(int error, @Nullable Throwable exception) {
                            hideProgressBar();
                            assert exception != null;
                            {
                                App.getInstance().logException(new Exception(exception), getClass());
                                errorMessage(exception.getMessage());
                            }
                        }
                    });
                } else {
                    hideProgressBar();
                    errorMessage(getString(R.string.msg_email_not_registered));
                }
            });
        } catch (Exception e) {
            hideProgressBar();
            App.getInstance().logException(e, getClass(), true);
            Utilities.getInstance().showDialog(this, getString(R.string.some_error_occurred), getString(R.string.msg_fail_login_play_service)).show();
        }
    }

    public void tryLogIn(String email, String pass) {
        FirebaseAuth.getInstance().signInWithEmailAndPassword(email, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
                if (task.isSuccessful() && currentUser != null) {
                    Log.w(TAG, "signInWithCustomToken:success", task.getException());
                    successResponse(currentUser);
                } else {
                    Log.w(TAG, "signInWithCustomToken:failure", task.getException());
                    errorResponse(0, task.getException());
                }
            }
        });
    }

2 个答案:

答案 0 :(得分:4)

尝试此代码

bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Validation.loginValidation(edtEmail, edtPass)) {
                    loginFirebase(edtEmail.getText().toString(), edtPass.getText().toString(), firebaseAuth);
                }
            }
        });




public void loginFirebase(String email, String pass, FirebaseAuth firebaseAuth) {
        progressDialog.setTitle("Please Wait.....");
        progressDialog.setMessage("Processing....");
        progressDialog.setCancelable(false);
        progressDialog.show();
        (firebaseAuth.signInWithEmailAndPassword(email, pass)).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity(), "Login Successful", Toast.LENGTH_LONG).show();
                    Intent i = new Intent(getActivity(), DrawerMain.class);
                    startActivity(i);
                    edtEmail.setText("");
                    edtPass.setText("");
                } else {
                    Log.e("Error", task.getException().toString());
                    Toast.makeText(getActivity(), task.getException().getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

希望能对您有所帮助!

谢谢。

答案 1 :(得分:2)

使用addOnCompleteListener时,始终需要检查task是否成功。

尝试以下操作,

somethingTask().addOnCompleteListener(task -> {
    if (!task.isSuccessful()) {
        Log.wtf(TAG, "somethingTask:ERROR", task.getException());
        return;
    }
    Log.i(TAG, "somethingTask:SUCCESS");
    // Now we are logged in, let's write next code!
}

我正在根据您的错误堆栈跟踪来写此答案,该跟踪在getResult方法中引发了tryLogin的错误。

+ ,因为addOnCompleteListener是一种asynchronous方法,所以您不会捕获这种异常。