我正在使用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
方法。我不确定这是怎么回事。 最佳答案
哇!
错误在isEmailAvailable方法中。
查找是否为异步方法。
它的工作方式是这样的:您调用它,它开始工作并立即返回,直到实际产生任何结果为止。
稍后,它将调用回调方法。所以,当你这样做
return isAvailable;
在该行上,isAvailable始终为false,因为它仍未更改。
您应该做一些异步工作。
1.调用Backendless.Persistence.of(BackendlessUser.class).find(...)
2.等待答案。您只会知道结果
public void handleResponse(BackendlessCollection<BackendlessUser> userBackendlessCollection) {
isAvailable = userBackendlessCollection.getData().isEmpty();
}
3.刚做完剩下的工作
isAvailable = userBackendlessCollection.getData().isEmpty();
创建方法doCreateUser,将您的“ if”主体放入其中,并在isAvailable = userBackendlessCollection.getData()。isEmpty();之后从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();
}
});
}
关于java - Android-应该在未运行任务的情况下,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38024161/