我正在尝试在Android中开发登录活动,并且我使用followind方法登录:
private void attemptLogin() {
/*if (mAuthTask != null) {
return;
}*/
// Reset errors.
mEmailView.setError(null);
mPasswordView.setError(null);
// Store values at the time of the login attempt.
String email = mEmailView.getText().toString();
String password = mPasswordView.getText().toString();
boolean cancel = false;
View focusView = null;
// Check for a valid password, if the user entered one.
if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
mPasswordView.setError(getString(R.string.error_invalid_password));
focusView = mPasswordView;
cancel = true;
}
// Check for a valid email address.
if (TextUtils.isEmpty(email)) {
mEmailView.setError(getString(R.string.error_field_required));
focusView = mEmailView;
cancel = true;
} else if (!isEmailValid(email)) {
mEmailView.setError(getString(R.string.error_invalid_email));
focusView = mEmailView;
cancel = true;
}
if (cancel) {
// There was an error; don't attempt login and focus the first
// form field with an error.
focusView.requestFocus();
} else {
// Show a progress spinner, and kick off a background task to
// perform the user login attempt.
showProgress(true);
RetrofitAPIService retrofitAPIService = RetrofitAPIService.aRetrofitApiService();
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
/*authService.login(email, password);*/
UserAuthCommand userAuthCommand = new UserAuthCommand(email, password);
UserProfile userProfile = retrofitAPIService.authorizeUser(userAuthCommand);
Runnable runnable = new Runnable() {
@Override
public void run() {
if (userProfile != null) {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Bad", Toast.LENGTH_SHORT).show();
}
}
};
LoginActivity.this.runOnUiThread(runnable);
/* final Looper looper = Looper.myLooper();
looper.quit();*/
}
}).start();
//new UserLoginTask(email, password).execute((Void) null);
}
}
我通过点击登录按钮调用此方法,覆盖设置setOnClickListener并重写onClick()。
我的问题是,当我输入一些错误的凭据时,Toast会在屏幕上显示,但后端会进入无限循环。我做了一些调试,似乎当前线程从Looper.class进入loop()方法。
任何人都可以帮助我吗?很多!
答案 0 :(得分:1)
我建议你使用AsyncTask来自动管理线程和uithread回调。
例如
private class LoginTask extends AsyncTask<ParamsType, ProgressType, ResultType> {
protected ResultType doInBackground(ParamsType... params) {
// This method runs in a background thread
return result;
}
protected void onPostExecute(ResultType result) {
// This method runs in UiThread
}
}
将您的登录逻辑放在doInBackground()
方法中,将结果逻辑放在onPostExecute()
中。并运行:
new LoginTask().execute(params);
希望这有帮助。