onPostExecute在doInBackground之前执行 [英] onPostExecute is getting executed before doInBackground

查看:162
本文介绍了onPostExecute在doInBackground之前执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题.我尝试搜索答案,但是没有任何答案可以帮助我,因此我在这里提出了这个问题.任何回应将不胜感激!

I am stuck with the following issue. I tried searching for answers but none of the responses could help me so i am asking this question here. Any response will be greatly appreciated!

我在AsyncTask下使用它来使用Firebase API公开的登录方法.但是,当我单击登录按钮时调用新的LoginOperation().execute()时,我看不到预期的结果.我粘贴下面的代码和Logcat输出.

I am using this below AsyncTask to use the login methods exposed by the Firebase API. However when i invoke new LoginOperation().execute() at the click of login button, i am not seeing the expected results. I am pasting the code below and the Logcat output.

我想知道为什么onPostExecute在doInBackground之前被执行?请注意,我使用的是有效的电子邮件ID&密码,所以我应该能够正确登录.

I would like to know why onPostExecute is getting executed before doInBackground? Please note i am using a valid email id & password, so i should have been able to login properly.

代码:

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) {}
}

登录时调用的代码单击:

Code invoked on Login click:

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 abc@gmail.com
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,因为您在doInBackground中使用的类似乎已经在后台线程中执行了其任务.将doInBackground中的代码移动到UI线程中,然后将代码从onPostExecute移动到SimpleLoginAuthenticationHandler.

You don't need your AsyncTask because the class you are using in doInBackground seems to already perform its task in a background thread. Move the code in doInBackground somewhere to the UI thread and move your code from onPostExecute into the SimpleLoginAuthenticationHandler.

当前,onPostExecuted被立即调用,因为它只是启动一个新的后台线程并立即返回.

Currently, onPostExecuted is called immediately, because it just starts a new background thread and comes back immediately.

这篇关于onPostExecute在doInBackground之前执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆