2014-01-27 167 views
0

我遇到以下問題。我試圖尋找答案,但沒有任何答覆可以幫助我,所以我在這裏問這個問題。任何迴應將不勝感激!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 
+0

爲了確保您沒有搞亂任何方法簽名,請在'AsyncTask'中覆蓋所有您要覆蓋的方法之上放置'@ Override'註釋。 – NasaGeek

+0

你是否修改過Sdk? = 0! Jk =) – Jorgesys

回答

1

您不需要您的AsyncTask,因爲您在doInBackground中使用的類似乎已在後臺線程中執行其任務。將代碼doInBackground移到UI線程的某處,並將代碼從onPostExecute移動到SimpleLoginAuthenticationHandler

目前,onPostExecuted被立即調用,因爲它只是啓動一個新的後臺線程並立即返回。

+0

非常感謝您的建議!我遵循你的步驟,現在我正在獲得所需的輸出。非常感謝您的幫助。 – user3199806

+0

我知道這是兩歲,但任何人都可以告訴如何@FD_會知道類內doInBackground已經在另一個後臺線程?你能通過閱讀上面的代碼來判斷嗎? – Gil

+0

@Gil我認爲這只是一個受過教育的猜測,它基於事件處理程序被傳遞給在那裏被調用的方法。 –

1

這很可能是因爲SimpleLoginAuthenticatedHandler.authenticated()異步執行。它看起來根本不需要AsyncTask。

+0

謝謝你的建議。我刪除了異步任務,並按照FD_給出的建議進行操作。感謝您的幫助。 – user3199806

1

我的猜測是loginWithEmail()Thread內運行。這意味着當你調用這個方法時,線程確實運行了,但並不意味着它已經結束了它的執行,所以也許它運行平穩,你回來了,完成了方法,並且它加入了onPostExecute()方法。

如果是這樣,我會刪除AsyncTask,這是沒有必要的。

+0

謝謝你的幫助。是的,loginWithEmail在一個線程中運行。所以我刪除了AsyncTask,並遵循FD_建議,並能夠正確運行我的代碼。 – user3199806

相關問題