1

I want to cancel the AsyncTask when the json request comes with a null value, and display the Toast message.

private class PostTask extends AsyncTask<String, Integer, String>
{
    //Before running code in the separate thread
    @Override
    protected void onPreExecute() 
    {

        progressDialog = new ProgressDialog(login.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("please wait...");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(false);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.show();
    }
    //The code to be executed in a background thread.
    @Override
    protected  String doInBackground(String... params) 
    {
        String url=params[0];
        Parser parse = new Parser();
        try{    
            String email = textinput.getText().toString();
            String pass = password.getText().toString();
            JSONObject JsonString = parse.getJSONFromUrl(url,email,pass);

            //String email = JsonString.getString("email");
            Constants.ID = JsonString.getString("id");

        }
         catch (JSONException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
              }

        catch (NullPointerException e){

             publishProgress(1);
            //Toast.makeText(login.this, "Invalid credentials", Toast.LENGTH_LONG).show();
        }
         return "All Done!";
    }
    //Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) 
    {
        //set the current progress of the progress dialog
    //  progressDialog.setProgress(values[0]);  
        //Toast.makeText(login.this, "Invalid credentials", Toast.LENGTH_LONG).show();
    }
    //after executing the code in the thread
    @Override
      protected void onPostExecute(String result) {

          super.onPostExecute(result);
          startActivity(new Intent("com.example.mysampleapp.DASHBOARDTAB"));
          progressDialog.dismiss();      
     }
}

Is there any cancel method to cancel the async task if I get null in JSON request and display the message in the same activity from where I am calling the AsyncTask?

CocoNess
  • 4,213
  • 4
  • 26
  • 43
Amit Koranne
  • 141
  • 2
  • 11
  • Check this Link http://stackoverflow.com/questions/4748964/android-cancel-asynctask-forcefully?lq=1 –  Dec 19 '12 at 12:14
  • possible duplicate of [Android: Cancel Async Task](http://stackoverflow.com/questions/6039158/android-cancel-async-task) – JaredMcAteer Dec 19 '12 at 13:48
  • an example for cancelling an asynctask http://www.quicktips.in/correct-way-to-cancel-an-asynctask-in-android/ – Deepak Swami Jun 29 '16 at 17:18

2 Answers2

2

Try returning an error code if the JSON is null, something like:

   catch (NullPointerException e){

         publishProgress(1);
        return "Error";
    }

Then in onPostExecute():

@Override
  protected void onPostExecute(String result) {
      super.onPostExecute(result);

      if(result.equals("Error")) {
          //Error
      } else {
          startActivity(new Intent("com.example.mysampleapp.DASHBOARDTAB"));
          progressDialog.dismiss();      
      }
 }
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
1

Try this:

postTask = new PostTask();

if (postTask != null)
 {
   postTask.cancel(true);
   postTask = null;
}

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37