0

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).

Here is part of my code, where I set the setOnCancelListener of the ProgressDialog: (client is an instance of AsyncHttpClient )

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
                    }
                });
    }
Carla Urrea Stabile
  • 869
  • 1
  • 11
  • 35

3 Answers3

2
client.post(context,getAbsoluteUrl(LOGIN_PATH), params,
            new JsonHttpResponseHandler();

add a context parameter in your post.

Lincoln Sumauto
  • 219
  • 2
  • 3
1

Try to change the method to:

client.cancelAllRequests(true);

For more documentation: http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html#cancelAllRequests(boolean)

0
client.post(context, apiName, stringEntity, "text/plain",
                    uploadResponseHandler(callBack, apiNumber, requestId, context, resultClass)).setTag("uploadFile");

and then

    client.cancelRequestsByTAG("uploadFile",true);
ROHIT LIEN
  • 497
  • 3
  • 8