java - 如何循环异常直到用户成功或错误

标签 java recursion

我正在使用下面的代码使用电子邮件和密码登录用户,但我在 java 方面并不那么先进。我正在尝试循环任务异常,直到用户成功。

  public void userLogin() {
    String email = etEmail.getText().toString().trim();
    final String password = etPassword.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        //email is empty
        Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
        return ;
    }

    if(TextUtils.isEmpty(password)){
        //password is empty
        Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
        return ;
    }

    progressDialog.setMessage("Login Please wait...");
    progressDialog.show();

    firebaseAuth.signInWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    progressDialog.dismiss();
                    performTask();

                }
            });

}

public void performTask() {

    Task task = onComplete(@NonNull Task<AuthResult> task);


    if (task.isSuccessful()) {
        finish();
        userVerified();
    } else {
        String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();

        switch (errorCode) {

            case "ERROR_INVALID_EMAIL":
                Toast.makeText(Signin.this, "The email address is badly formatted.", Toast.LENGTH_LONG).show();
                etEmail.setError("The email address is badly formatted.");
                etEmail.requestFocus();
                break;

            case "ERROR_WRONG_PASSWORD":
                Toast.makeText(Signin.this, "The password is invalid ", Toast.LENGTH_LONG).show();
                etPassword.setError("password is incorrect ");
                etPassword.requestFocus();
                etPassword.setText("");
                break;

            case "ERROR_USER_MISMATCH":
                Toast.makeText(Signin.this, "The supplied credentials do not correspond to the previously signed in user.", Toast.LENGTH_LONG).show();
                break;

            case "ERROR_REQUIRES_RECENT_LOGIN":
                Toast.makeText(Signin.this, "This operation is sensitive and requires recent authentication. Log in again before retrying this request.", Toast.LENGTH_LONG).show();
                break;

            case "ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL":
                Toast.makeText(Signin.this, "An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.", Toast.LENGTH_LONG).show();
                break;

            case "ERROR_EMAIL_ALREADY_IN_USE":
                Toast.makeText(Signin.this, "The email address is already in use by another account.   ", Toast.LENGTH_LONG).show();
                etEmail.setError("The email address is already in use by another account.");
                etEmail.requestFocus();
                break;
            case "ERROR_USER_NOT_FOUND":
                Toast.makeText(Signin.this, "Your account not registered", Toast.LENGTH_LONG).show();
                break;
        }

        performTask();
    }
}

当我尝试这个循环时,应用程序将停止。我尝试让应用程序不会崩溃。我不知道我想在 Task task = getTask(); 中放入什么;

最佳答案

我相信递归将是解决您的问题的一个很好的解决方案,如下所示。假设您从某个 API 获取 task(让 getTaskDetails();)。然后,每次出现错误时,使用现有的 switch case 显示错误,然后再次调用方法 performTask() 再次执行任务并执行相同的逻辑,直到未收到正确的详细信息或任务不成功。

    public void userLogin() {
    String email = etEmail.getText().toString().trim();
    final String password = etPassword.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        //email is empty
        Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
        return ;
    }

    if(TextUtils.isEmpty(password)){
        //password is empty
        Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
        return ;
    }

    progressDialog.setMessage("Login Please wait...");
    progressDialog.show();

    firebaseAuth.signInWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    progressDialog.dismiss();
                    performTask(task);

                }
            });

}

public void performTask(Task<AuthResult> task) {



    if (task.isSuccessful()) {
        finish();
        userVerified();
    } else {
        String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();

        switch (errorCode) {

            case "ERROR_INVALID_EMAIL":
                Toast.makeText(Signin.this, "The email address is badly formatted.", Toast.LENGTH_LONG).show();
                etEmail.setError("The email address is badly formatted.");
                etEmail.requestFocus();
                break;

            case "ERROR_WRONG_PASSWORD":
                Toast.makeText(Signin.this, "The password is invalid ", Toast.LENGTH_LONG).show();
                etPassword.setError("password is incorrect ");
                etPassword.requestFocus();
                etPassword.setText("");
                break;

            case "ERROR_USER_MISMATCH":
                Toast.makeText(Signin.this, "The supplied credentials do not correspond to the previously signed in user.", Toast.LENGTH_LONG).show();
                break;

            case "ERROR_REQUIRES_RECENT_LOGIN":
                Toast.makeText(Signin.this, "This operation is sensitive and requires recent authentication. Log in again before retrying this request.", Toast.LENGTH_LONG).show();
                break;

            case "ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL":
                Toast.makeText(Signin.this, "An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.", Toast.LENGTH_LONG).show();
                break;

            case "ERROR_EMAIL_ALREADY_IN_USE":
                Toast.makeText(Signin.this, "The email address is already in use by another account.   ", Toast.LENGTH_LONG).show();
                etEmail.setError("The email address is already in use by another account.");
                etEmail.requestFocus();
                break;
            case "ERROR_USER_NOT_FOUND":
                Toast.makeText(Signin.this, "Your account not registered", Toast.LENGTH_LONG).show();
                break;
        }

        userLogin();
    }
}

关于java - 如何循环异常直到用户成功或错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58345437/

相关文章:

java - 使用 Angularjs 从 Grails 后端获取 JSON 数据

c - 使我的递归代码不那么复杂

c++ - 递归逆函数

java - 将 Stream<T> 映射到正在计数的 Map<T, Long>?

java - 如何在java中访问对象列表元素?

java - 解决吃 bean 游戏中上下移动时出现的问题

javascript - 使用递归嵌套父子

python - 从递归函数导出封闭形式

php - 递归搜索并删除数组?

java - 直接向 Quarkus/RESTEasy Web 服务方法提供 SecurityIdentity