我遇到以下問題。我試圖尋找答案,但沒有任何答覆可以幫助我,所以我在這裏問這個問題。任何迴應將不勝感激!onPostExecute在doInBackground之前執行
我使用下面的AsyncTask來使用Firebase API公開的登錄方法。但是,當我點擊登錄按鈕調用新的LoginOperation()。execute()時,我沒有看到預期的結果。我粘貼下面的代碼和Logcat輸出。
我想知道爲什麼onPostExecute在doInBackground之前執行?請注意我正在使用有效的電子郵件ID &密碼,所以我應該能夠正確登錄。
代碼:
private class LoginOperation extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
try{
authClient.loginWithEmail(emailid.getText().toString(),password.getText().toString(), new SimpleLoginAuthenticatedHandler() {
public void authenticated(
com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
// There was an error logging into this account
loginStatus=false;
errorMsg=error.name();
Log.d(appName, "Inside if block in doInBackground of LoginOperation");
}
else {
// We are now logged in
loginStatus=true;
Log.d(appName, "Inside else block in doInBackground of LoginOperation");
}
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
return Boolean.valueOf(loginStatus);
}
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result.booleanValue()) {
toastMsg="User logged in successfully";
Log.d(appName, "Inside onPostExecute success of LoginOperation");
}
else
{
toastMsg="Error in login";
Log.d(appName, "Inside onPostExecute failure of LoginOperation");
}
TextView displayStatus = (TextView) findViewById(R.id.displayStatus);
displayStatus.setText(toastMsg);
}
protected void onPreExecute() {
super.onPreExecute();
}
protected void onProgressUpdate(Void... values) {}
}
代碼調用上登錄點擊:
public void onLogin(View arg0)
{
Log.d(appName, " email id is " + emailid.getText().toString());
Log.d(appName, " password is " + password.getText().toString());
try {
Boolean finalStatus= new LoginOperation().execute().get(5000, TimeUnit.MILLISECONDS);
Log.d(appName, " final Status is: " + finalStatus.booleanValue());
}
的logcat:
01-27 17:50:02.054: D/LOGIN(984): email id is [email protected]
01-27 17:50:02.054: D/LOGIN(984): password is abc123
01-27 17:50:02.082: D/LOGIN(984): final Status is: false
01-27 17:50:02.082: D/LOGIN(984): Inside onPostExecute failure of LoginOperation
01-27 17:50:05.502: D/LOGIN(984): Inside else block in doInBackground of LoginOperation
預期結果:
Inside else block in doInBackground of LoginOperation
Inside onPostExecute success of LoginOperation
爲了確保您沒有搞亂任何方法簽名,請在'AsyncTask'中覆蓋所有您要覆蓋的方法之上放置'@ Override'註釋。 – NasaGeek
你是否修改過Sdk? = 0! Jk =) – Jorgesys