未调用异步任务的onPostExecute方法

时间:2012-10-29 07:09:42

标签: android asynchronous android-asynctask

当使用连接到互联网的应用程序时,如果应用程序崩溃或在此之后被杀,我无法再次登录,我必须停止应用程序 - >我的应用 - >停止按钮再次登录。

当关注应用程序的debuging时,我的调试消息到了doInBackground方法的结尾,但我知道Async任务类在doInbackground方法之后调用onPostExecute,在我的例子中杀死或崩溃应用程序后这不是。

有人可以帮我解决这个问题吗?

    private class doAuthentication extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {



        Requester req = new Requester();
        String hash = req.loginByPIN(params[0]);//,VendingManagerActivity.this);

        return hash;
    }

    @Override
    protected void onPostExecute(String hash) {
        Log.d("Vending","In post Ex-> "+hash);
    //  myProgressDialog.dismiss();


        if (hash.equals("")) {

            final EditText kbdInput = (EditText) findViewById(R.id.kbdInput);

            kbdInput.setText("");

            displayMessage(getString(R.string.err_wrong_credentials));
            return;
        } else if (hash.equals("-")) {

            final EditText kbdInput = (EditText) findViewById(R.id.kbdInput);

            kbdInput.setText("");

            displayMessage(getString(R.string.err_no_permissions));
            return;
        } else {

            String[] resp = hash.split("#");


            Editor edit = mPrefs.edit();

            edit.putString("login_hash",resp[0]);// hash);
            edit.putString("password",resp[1]);//lbp.reset_pass);
            edit.commit();

            Intent mainScreenActivity = new Intent(
                    VendingManagerActivity.this, MainScreenActivity.class);

            startActivity(mainScreenActivity);

            finish();
        }
    }
}

请求者类 - &gt;方法loginByPIN - &gt;

    public String loginByPIN(String pin_code)
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
    nameValuePairs.add(new BasicNameValuePair("module", "tablet"));
    nameValuePairs.add(new BasicNameValuePair("method", "loginByPin"));
    nameValuePairs.add(new BasicNameValuePair("pin", pin_code));
    String xmlData = getRequest(API_ADDRESS, nameValuePairs);

    String hash = "";
    String response = "";

    try {

        Serializer serializer = new Persister();        

        Reader reader = new StringReader(xmlData);
        LoginByPin lbp = 
            serializer.read(LoginByPin.class, reader, false);

        hash = lbp.hash;

        Requester.HASH = hash;

        Log.d("Vending", "HASH received: " + hash);

        response = hash +"#"+lbp.reset_pass;



    } catch (Exception e) {

        Log.d("Vending",e.getMessage().toString());
        Serializer serializer = new Persister();        

        Reader reader = new StringReader(xmlData);

        try {

            Error err = 
                serializer.read(Error.class, reader, false);

            Integer errorCode = err.code;
            String errorText = err.text;

            Log.e("Vending", "ERROR: " + errorText + "(" + Integer.toString(errorCode) + ")");

            if (err.code == 200)
                return "-";

        } catch (Exception e2) {

            Log.e("Vending", "ERROR: Invalid output returned from API");

        }

    }
    Log.d("Vending","Before returning-> "+hash);
    return response;//hash;
}

谢谢!

编辑: 不要以这种方式更改代码

@Override
    protected String doInBackground(String... params) {



        Requester req = new Requester();
        String hash = req.loginByPIN(params[0]);//,VendingManagerActivity.this);
        Log.d("Vending","Before doInBackground ends-> "+hash);
        return hash;
    }

    @Override
    protected void onPostExecute(String hash) {
        Log.d("Vending","In post Ex-> "+hash);
        myProgressDialog.dismiss();

并且logcat输出为

10-29 10:26:06.278: D/Vending(25714): Before returning-> peq8qkjvee7v4nhci8v3dub293
10-29 10:26:06.278: D/Vending(25714): Before doInBackground ends-> peq8qkjvee7v4nhci8v3dub293#qwerty123

1 个答案:

答案 0 :(得分:1)

如果您的应用在执行doInBackground()时崩溃,则不会调用onPostExecute()

onPostExecute()仅在成功完成doInBackground()方法后调用。