eng.ahmed
eng.ahmed

Reputation: 925

how to stop AsyncTask if some condition is true

I use asynctask to get json data from remote url but sometimes this url return errors in json data like this :

{
   "error": {
      "message": "(#803) Some of the aliases you requested do not exist: RNN.NES",
      "type": "OAuthException",
      "code": 803
   }
}

in doInBackground, i check if there an error by json ..

if(true) {
    //cancel asynctask and show toast with message i use 
    cancel(true)
}

but i do that the condititon is true but it is not cancelled

this is my code :

private class MyAsyncTask extends AsyncTask<String, Void, Void> {

        ProgressDialog mProgressDialog;

        @Override
        protected void onPostExecute(Void result) {
            db = new DAOPages(context);
            db.open();
            db.addPage(name, fid, picName);
            getPagePrefs(fid);
            db.close();
            mProgressDialog.dismiss();

            Intent intent = new Intent(context, PagePrefsActivity.class);
            intent.putExtra("fPageid", fid);
            context.startActivity(intent);

        }

        @Override
        protected void onPreExecute() {

            mProgressDialog = ProgressDialog.show(context, "Loading...",
                    "Data is Loading...");
        }

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

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(params[0]);
            // addPageData(params[0]);
            try {

                if(json.getJSONObject("error") != null) {
                    Log.e("error", "true");
                    cancel(true);
                }
                name = json.getString("name");
                fid = json.getString("id");                                 

                String picture = json.getJSONObject("picture")
                        .getJSONObject("data").getString("url");
                picName = downloadImage(picture, fid);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

Now how can i stop that and show toast ???

thanks in advance

Upvotes: 0

Views: 1083

Answers (2)

Steve Benett
Steve Benett

Reputation: 12933

The method cancel(true) doesn't stop the AsyncTask. It just invokes onCancelled() instead of onPostExecute() after doInBackGround returns. You can use AsyncTask.isCancelled(), which will return true if you cancel the AsyncTask, to prevent further processing in doInBackground().

So override onCancelled() and put your Toast in there.

private class MyAsyncTask extends AsyncTask<String, Void, Void> {

        ProgressDialog mProgressDialog;

        @Override
        protected void onPostExecute(Void result) {
            db = new DAOPages(context);
            db.open();
            db.addPage(name, fid, picName);
            getPagePrefs(fid);
            db.close();
            mProgressDialog.dismiss();

            Intent intent = new Intent(context, PagePrefsActivity.class);
            intent.putExtra("fPageid", fid);
            context.startActivity(intent);

        }

        @Override
        protected void onPreExecute() {

            mProgressDialog = ProgressDialog.show(context, "Loading...",
                    "Data is Loading...");
        }

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

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(params[0]);
            // addPageData(params[0]);
            try {

                if(json.getJSONObject("error") != null) {
                    Log.e("error", "true");
                    cancel(true); // after this AsyncTask.isCancelled() returns true
                }

                if(!this.isCancelled()){ // if it's true, prevent further processing
                     name = json.getString("name");
                     fid = json.getString("id");                                 

                     String picture = json.getJSONObject("picture")
                            .getJSONObject("data").getString("url");
                     picName = downloadImage(picture, fid);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        // will be called instead onPostExecute after a cancel
        @Override
        protected void onCancelled() {
            // Show your Toast 
        }
    }

Upvotes: 1

Abhishek Birdawade
Abhishek Birdawade

Reputation: 301

You can check using isCancelled() in onPostExecute() method and can show Toast in that method.Here is how you can do that -:

    @Override
    protected void onPostExecute(Void result) {
        if(isCancelled()) {
            Toast.makeText(context, "your text", Toast.LENGTH_SHORT).show();
            return;
        }
        db = new DAOPages(context);
        db.open();
        db.addPage(name, fid, picName);
        getPagePrefs(fid);
        db.close();
        mProgressDialog.dismiss();

        Intent intent = new Intent(context, PagePrefsActivity.class);
        intent.putExtra("fPageid", fid);
        context.startActivity(intent);

    }

Upvotes: 0

Related Questions