Java 如何循环异常直到用户成功或出错

Java 如何循环异常直到用户成功或出错,java,recursion,Java,Recursion,我使用下面的代码使用电子邮件和密码登录用户,但我在java方面没有那么先进。我正在尝试循环任务异常,直到用户成功 public void userLogin() { String email = etEmail.getText().toString().trim(); final String password = etPassword.getText().toString().trim(); if(TextUtils.isEmpty(email)){

我使用下面的代码使用电子邮件和密码登录用户,但我在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();
    }
}
    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();
    }
}
public void userLogin(){
字符串email=etEmail.getText().toString().trim();
最终字符串密码=etPassword.getText().toString().trim();
如果(TextUtils.isEmpty(电子邮件)){
//电子邮件是空的
Toast.makeText(这是“请输入电子邮件”,Toast.LENGTH_SHORT).show();
返回;
}
if(TextUtils.isEmpty(密码)){
//密码为空
Toast.makeText(这是“请输入密码”,Toast.LENGTH_SHORT).show();
返回;
}
设置消息(“登录请稍候…”);
progressDialog.show();
firebaseAuth.使用电子邮件和密码登录(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
progressDialog.disclose();
performTask();
}
});
}
public void performTask(){
Task Task=onComplete(@NonNull Task Task);
if(task.issusccessful()){
完成();
userVerified();
}否则{
字符串errorCode=((FirebaseAuthException)task.getException()).getErrorCode();
开关(错误代码){
案例“错误\u无效\u电子邮件”:
Toast.makeText(签名:“电子邮件地址格式不正确。”,Toast.LENGTH_LONG.show();
setError(“电子邮件地址格式不正确。”);
etEmail.requestFocus();
打破
案例“错误\错误\密码”:
Toast.makeText(签名为“密码无效”,Toast.LENGTH_LONG.show();
etPassword.setError(“密码不正确”);
etPassword.requestFocus();
etPassword.setText(“”);
打破
案例“错误\用户\不匹配”:
Toast.makeText(Signin.this,“提供的凭据与以前登录的用户不对应。”,Toast.LENGTH_LONG.show();
打破
案例“错误要求最近登录”:
Toast.makeText(Signin.this,“此操作敏感,需要最近的身份验证。请在重试此请求之前再次登录。”,Toast.LENGTH_LONG.show();
打破
案例“错误\u帐户\u存在\u且凭据不同”:
Toast.makeText(Signin.this,“已存在具有相同电子邮件地址但不同登录凭据的帐户。请使用与此电子邮件地址关联的提供程序登录。”,Toast.LENGTH_LONG).show();
打破
案例“错误电子邮件已在使用”:
Toast.makeText(签名为“电子邮件地址已被另一个帐户使用。”,Toast.LENGTH_LONG.show();
setError(“该电子邮件地址已被其他帐户使用。”);
etEmail.requestFocus();
打破
案例“错误用户未找到”:
Toast.makeText(签名为“您的帐户未注册”,Toast.LENGTH_LONG.show();
打破
}
performTask();
}
}

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

我相信递归将是解决以下问题的好方法。假设您正在从某个API(让
getTaskDetails();
)获取
task
)。然后,每次出现错误时,使用现有开关大小写显示错误,然后再次调用方法
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();

                }
            });

}

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();
    }
}
    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();
    }
}
public void userLogin(){
字符串email=etEmail.getText().toString().trim();
最终字符串密码=etPassword.getText().toString().trim();
如果(TextUtils.isEmpty(电子邮件)){
//电子邮件是空的
Toast.makeText(这是“请输入电子邮件”,Toast.LENGTH_SHORT).show();
返回;
}
if(TextUtils.isEmpty(密码)){
//密码为空
Toast.makeText(这是“请输入密码”,Toast.LENGTH_SHORT).show();
返回;
}
设置消息(“登录请稍候…”);
progressDialog.show();
firebaseAuth.使用电子邮件和密码登录(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
progressDialog.disclose();
执行任务;
}
});
}
公共void性能任务(任务){
if(task.issusccessful()){
完成();
userVerified();
}否则{
字符串errorCode=((FirebaseAuthException)task.getException()).getErrorCode();
开关(错误代码){
案例“错误\u无效\u电子邮件”:
Toast.makeText(签名:“电子邮件地址格式不正确。”,Toast.LENGTH_LONG.show();
setError(“电子邮件地址格式不正确。”);
etEmail.requestFocus();
打破
案例“错误\错误\密码”:
Toast.makeText(签名为“密码无效”,Toast.LENGTH_LONG.show();
etPassword.setError(“密码不正确”);
etPassword.requestFocus();
etPassword.setText(“”);
打破
案例“错误\用户\不匹配”:
Toast.makeText(Signin.this,“提供的凭据与以前登录的用户不对应。”,Toast.LENGTH_LONG.show();
打破
案例“错误要求最近登录”:
Toast.makeText(签名此,“此操作