无法在另一个AsyncTask的postexecute()中调用AsyncTask的execute()方法

时间:2014-12-12 06:16:07

标签: java android android-asynctask

我正在使用内部AsyncTasks类来根据结果发出服务器请求并更新我的UI。是否禁止从另一个AsyncTask的postexecute()调用execute()方法? (我在RatingHospital()的postexecute()中调用new updateRating()。execute(),但它不起作用:()

    public class RatingHospital extends AsyncTask<String, String, String> {


    JSONParser jsonParser = new JSONParser();

    private static final String RATING_URL = "http://2healthpal.fh2web.com/Server/rating.php";
    private static final String TAG_SUCCESS = "success";
    boolean hasRating;


    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

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

        //Background works              

        return null;
    }

    @Override
    protected void onPostExecute(String result) {



        if (hasRating==true)
            {
              showUpdateAlert();
            }
     else {

             showRatingDialog();

            //TODO
        }
    }

    private void showRatingDialog() {

        final Dialog d = new Dialog(Main.this);
        d.setTitle("User Feedback");
        d.setContentView(R.layout.dialog_rating);

        final RatingBar userRating = (RatingBar) d.findViewById(R.id.ratingBarHosp);
        Button saveRating = (Button) d.findViewById(R.id.btnSaveRating);

        saveRating.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View v) {

                ratingToSave = userRating.getRating();
                d.dismiss();
                new updateRating().execute();  //This line is creating problem
            }
        });


        d.show();

    }

    private void showUpdateAlert() {
        AlertDialog.Builder dBuilder = new AlertDialog.Builder(Main.this);

        dBuilder.setIcon(R.drawable.ic_dialog_alert);
        dBuilder.setTitle("Already Rated!!");
        dBuilder.setMessage("U have already rated this hospital.\n Do you want to update?");
        dBuilder.setCancelable(false);
        dBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        showRatingDialog();
                    }
                });

        dBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        AlertDialog quitAlert = dBuilder.create();
        quitAlert.show();

    }

}

class updateRating extends AsyncTask<String, String, String> {




    @Override
    protected void onPreExecute() {

        super.onPreExecute();

    }

    protected String doInBackground(String... param) {




        return null;
    }


    protected void onPostExecute(String... params) {



        if(success==1)
            Toast.makeText(getApplicationContext(), "Your feedback is saved successfully", Toast.LENGTH_LONG).show();


    }


}

1 个答案:

答案 0 :(得分:0)

所以你可以通过以下方式完成:

    updateRating data = new updateRating();
    data.execute();