如何在onPostExecute中获得响应?

时间:2014-09-10 08:29:11

标签: android

我正在尝试在onPostExecute方法中获得响应。目的是在测试结果值后显示一个AlertDialog,所以,我已经在doInBackground中完成了,但我认为它不是逻辑?我想在onPostExecute中做到这一点,但仍然不知道如何得到响应。这是我的代码

//@Override
    protected void onPostExecute(final Boolean success/*,String result*/) {
        //LoginTask = null;
        //showProgress(false);
        //delegate.processFinish(result);

        if (success) {
           /* AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Succes !");
            builder.setMessage("your new password has been sent to the email address you specified. \nThink to change it later!")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();*/
            finish();
        } else {
            //password.setError(getString(R.string.error_incorrect_password));
            //password.requestFocus();
            /*AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Error !");
            builder.setMessage("This email does not exist")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();*/

        }

    }

2 个答案:

答案 0 :(得分:1)

你可以像这样使用async:

public class asy extends AsyncTask<Void, Void, String>{

    @Override
    protected String doInBackground(Void... params) {
        // do your work,and return result in string
        //you can send null if not success else return string
        if(your work was true)
            return result;
        else
            return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //do any thing with result
        if(result == null){
            //not success
        }else{
            //success
        }
    }

}

答案 1 :(得分:0)

  

记得使用布尔值(包装类)而不是布尔数据类型

public class asyncTask extends AsyncTask<Void, Void, Boolean>{

    @Override
    protected Boolean doInBackground(Void... params) {
        //your background code
        if(background code is true)
            return true;
        else
            return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if(result == true){
            //success
        }else if(result == false{
            //not success
        }
    }

}