Android - 任务未运行时应该

时间:2016-06-25 01:39:04

标签: java android android-asynctask

我使用Backendless来存储我的应用用户的数据。 Backendless只能选择使您的电子邮件或用户名唯一。我使用户名字段是唯一的,这是用户将用于登录的内容。电子邮件可以重复使用,因此我在注册前制作方法以检查电子邮件是否存在。

这是createUser方法,一开始就调用该方法,以便在启动后立即检查电子邮件的可用性:

private void createUser() {
    showProgressDialog();

    if (isEmailAvailable(inputEmail.getText().toString().trim())) {
        BackendlessUser user = new BackendlessUser();
        user.setProperty(BackendSettings.USERNAME_KEY, inputUsername.getText().toString().trim());
        user.setEmail(inputEmail.getText().toString().trim());
        user.setPassword(inputPassword.getText().toString());

        Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
            @Override
            public void handleResponse(BackendlessUser backendlessUser) {
                hideProgressDialog();
                Toast.makeText(CreateAccountActivity.this, BackendSettings.REGISTER_SUCCESS_MESSAGE, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void handleFault(BackendlessFault backendlessFault) {
                hideProgressDialog();
                String errorCode = backendlessFault.getCode();
                String errorMessage;
                switch (errorCode) {
                    case "3040":
                        errorMessage = BackendSettings.ERROR_3040;
                        break;
                    case "3033":
                        errorMessage = BackendSettings.ERROR_3033;
                        break;
                    default:
                        errorMessage = "An unknown error occurred. Try again.";
                }
                Toast.makeText(CreateAccountActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
    } else {
        hideProgressDialog();
        Toast.makeText(CreateAccountActivity.this, BackendSettings.ERROR_EMAIL_TAKEN, Toast.LENGTH_SHORT).show();
    }
}

这是检查电子邮件可用性的方法:

private boolean isAvailable;
private boolean isEmailAvailable(String email) {
    isAvailable = false;

    String whereClause = "email = '" + email + "'";
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    dataQuery.setWhereClause(whereClause);
    Backendless.Persistence.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
        @Override
        public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
            isAvailable = userBackendlessCollection.getData().isEmpty();
        }

        @Override
        public void handleFault(BackendlessFault backendlessFault) {
            Toast.makeText(CreateAccountActivity.this, backendlessFault.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    return isAvailable;
}

好的,所以我总是收到电子邮件已被拍摄的消息,即使它不是。因此,我决定在Toast方法中添加handleResponse消息isEmailAvailble。如果电子邮件可用,我就打印出来。所以我首先从createUser中的else子句中得到消息,说该电子邮件已经被采用,然后我从isEmailAvailable方法获得了消息。似乎isEmailAvailable方法之后正在运行。我不确定它出了什么问题。

2 个答案:

答案 0 :(得分:1)

吁! 错误发生在isEmailAvailable方法中。

查找asyncronous方法。 它的工作方式如下:你调用它,它开始工作并立即返回,然后才真正产生结果。 后来它调用了回调方法。所以,当你做的时候

    return isAvailable;

isAvailable在该行上始终为false,因为它仍未更改。

你应该做一些异步工作。 1.调用Backendless.Persistence.of(BackendlessUser.class).find(...) 等待回答。您只能在

中知道结果
   public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
       isAvailable = userBackendlessCollection.getData().isEmpty();
   }

3。

之后完成剩下的工作
    isAvailable = userBackendlessCollection.getData().isEmpty();

在isAvailable = userBackendlessCollection.getData()。isEmpty();

之后创建一个方法doCreateUser,将你的主体“if”放入其中并从handleResponse调用它

使用此代码:

    private void createUser() {
        showProgressDialog();
        checkEmailAvailable(inputEmail.getText().toString().trim());
    }

    private void checkEmailAvailable(String email) {
        String whereClause = "email = '" + email + "'";
        BackendlessDataQuery dataQuery = new BackendlessDataQuery();
        dataQuery.setWhereClause(whereClause);
        Backendless.Persistence.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
            @Override
            public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
                isAvailable = userBackendlessCollection.getData().isEmpty();
                if (isAvailable){
                    doCreateuser();
                } else {
                    hideProgressDialog();
                    Toast.makeText(CreateAccountActivity.this, BackendSettings.ERROR_EMAIL_TAKEN, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void handleFault(BackendlessFault backendlessFault) {
                Toast.makeText(CreateAccountActivity.this, backendlessFault.getMessage(), Toast.LENGTH_SHORT).show();
                hideProgressDialog();
            }
        });
    }

    private void doCreateuser(){
        BackendlessUser user = new BackendlessUser();
        user.setProperty(BackendSettings.USERNAME_KEY, inputUsername.getText().toString().trim());
        user.setEmail(inputEmail.getText().toString().trim());
        user.setPassword(inputPassword.getText().toString());

        Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
            @Override
            public void handleResponse(BackendlessUser backendlessUser) {
                hideProgressDialog();
                Toast.makeText(CreateAccountActivity.this, BackendSettings.REGISTER_SUCCESS_MESSAGE, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void handleFault(BackendlessFault backendlessFault) {
                hideProgressDialog();
                String errorCode = backendlessFault.getCode();
                String errorMessage;
                switch (errorCode) {
                    case "3040":
                        errorMessage = BackendSettings.ERROR_3040;
                        break;
                    case "3033":
                        errorMessage = BackendSettings.ERROR_3033;
                        break;
                    default:
                        errorMessage = "An unknown error occurred. Try again.";
                }
                Toast.makeText(CreateAccountActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
    }

答案 1 :(得分:0)

对Backendless的调用是异步的,这就是为什么你的方法总是返回值为isAvailable的原因初始化为(这里为false)。在返回正确的值之前,您需要与Semaphore进行同步以阻止。

private boolean isEmailAvailable(String email) {
boolean isAvailable = false;
String whereClause = "email = '" + email + "'";
final Semaphore s = new Semaphore(0);
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause);

Backendless.Persistence.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
    @Override
    public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
        isAvailable = userBackendlessCollection.getData().isEmpty();
        s.release();
    }

    @Override
    public void handleFault(BackendlessFault backendlessFault) {
        Toast.makeText(CreateAccountActivity.this, backendlessFault.getMessage(), Toast.LENGTH_SHORT).show();
        s.release();
    }
});
try{
    s.aquire();
}catch(Exception e){
}
return isAvailable;

}