0

When use my application which connects to internet , if the app crash or be killed after this i cannot login again, i must to stop from applications-> my app - > stop button to login again.

When follow debuging of app ,my debug messages comes to the end of doInBackground method, but i knows that Async task class call to onPostExecute after doInbackground method,in my example after killing or crashing app this is not do.

Can anybody help me with this issue ?

    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();
        }
    }
}

Requester class -> method loginByPIN ->

    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;
}

Thanks!

Edit : not change the code in this way

@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();

and the logcat output is

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
4

1 回答 1

1

If your app crashes while doInBackground() is executing, then onPostExecute() will not be called.

onPostExecute() is only called after the successful completion of doInBackground() method.

于 2012-10-29T07:11:38.913 回答