2014-01-27 86 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

相关问题