title: Cancel AsyncHttpClient request when progress dialog is cancelled Android
body: <p>I'm making requests from my android app to a server and I am using loopj's AsyncHttpClient library to do so. I've trying to cancel a request if the progress dialog is cancelled (on back key pressed) but what it is actually happening is, the ProgressDialog gets cancelled but the request keeps working on background and when it finishes it takes me to another activity (which is fine because it is the behaviour I want when the progress dialog is not cancelled). </p>
  
  <p>Here is part of my code, where I set the setOnCancelListener of the ProgressDialog: (client is an instance of AsyncHttpClient )</p>
  
  <pre><code>public static void login(final String email, final String password,
              final Context context, final Context appContext, final Resources res) {
  
          prgDialog = new ProgressDialog(context);
          prgDialog.setMessage(res.getString(R.string.dialog_please_wait));
          prgDialog.setCancelable(true);
          prgDialog.show();
          prgDialog.setOnCancelListener(new OnCancelListener() {
  
              @Override
              public void onCancel(DialogInterface dialog) {
                  client.cancelRequests(context, true);
              }
          });
  
          cookieStore = new PersistentCookieStore(appContext);
          client.setCookieStore(cookieStore);
  
          RequestParams params = new RequestParams();
          params.put("user_session[email]", email);
          params.put("user_session[password]", password);
  
          client.addHeader("Accept", HEADER);
  
          client.post(getAbsoluteUrl(LOGIN_PATH), params,
                  new JsonHttpResponseHandler() {
  
                      @Override
                      public void onFailure(int statusCode,
                              org.apache.http.Header[] headers,
                              java.lang.String responseString,
                              java.lang.Throwable throwable) {
                          prgDialog.hide();
                          if (statusCode == 404) {
                              Toast.makeText(context,
                                      res.getString(R.string.error_404),
                                      Toast.LENGTH_LONG).show();
                          } else if (statusCode == 500) {
                              Toast.makeText(context,
                                      res.getString(R.string.error_500),
                                      Toast.LENGTH_LONG).show();
                          } else if (statusCode == 401) {
                              Toast.makeText(context,
                                      res.getString(R.string.login_401),
                                      Toast.LENGTH_LONG).show();
                          } else {
                              Toast.makeText(
                                      context,
                                      "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]",
                                      Toast.LENGTH_LONG).show();
                          }
                      }
  
                      @Override
                      public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
                          if (statusCode == 200) {
                  //do something
                      }
                  });
      }
  </code></pre>