square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.15k stars 7.3k forks source link

onFailure(...) not getting called on call.cancel() #2252

Closed ankitjain10 closed 7 years ago

ankitjain10 commented 7 years ago

What kind of issue is this?

ankitjain10 commented 7 years ago

void getTotalPayableCappingAmount(final boolean isRemoveClicked, final boolean isAddClicked) { mIsRemoveClicked = isRemoveClicked; mIsAddClicked = isAddClicked; // AlertUtils.showProgressDialog(CartActivity.this, null, "", false); String inputJsonForCapping = jsonHelper1.createJsonforCapping(cartHelper.getDiscount()); final Call cappedAmountCall = apiHelper.calculateCapping("application/json", getResources().getString(R.string.token), inputJsonForCapping); final ProgressBar amountPayableProgress = (ProgressBar) findViewById(R.id.amountPayableProgress);

    if (!mIsLoading) {
        mIsLoading = true;
        amountPayableProgress.setVisibility(View.VISIBLE);
        placeOrderButton.setBackgroundColor(getResources().getColor(R.color.colorPrimarywithOpacity));
        placeOrderButton.setText("Calculating basket...");
        placeOrderButton.setClickable(false);
        totalTextView.setVisibility(View.GONE);

        cappedAmountCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                mIsLoading = false;
                if (!isRemoveClicked) {
                    mIsRemoveClicked = true;
                }
                if (!isAddClicked) {
                    mIsAddClicked = true;
                }
                if (mIsAddClicked && mIsRemoveClicked) {
                    if (!response.isSuccessful()) {
                        ToastUtils.showShortToastSafe(CartActivity.this, "on success fail");
                        return;
                    }
                    if (response.body() != null) {
                        ToastUtils.showShortToastSafe(CartActivity.this, "on success body");

                        double totalAmountFromResponse = jsonHelper1.getTotalCappedFromResponse(response.body());
                        amountPayableProgress.setVisibility(View.GONE);
                        totalTextView.setVisibility(View.VISIBLE);
                        totalTextView.setText("₹ " + (int) totalAmountFromResponse);
                        cartHelper.setUserAmountPayable(totalAmountFromResponse);
                        placeOrderButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                        placeOrderButton.setClickable(true);
                        placeOrderButton.setText("Confirm Order");
                        totalStaticTextView.setText("Total :");
                        CartHelper2 cartHelper2 = new CartHelper2();
                        checkMinOrderAmount(cartHelper2.getCustomerPayableCappedwithoutDisAndDeliveryCharges());
                        taxesAndCapping.setText("₹ " + String.format("%.2f", cartHelper.getTaxesfromRevalue()));
                        taxesAndCapping.setVisibility(View.VISIBLE);
                        taxesAndAdjustmentTextView.setVisibility(View.VISIBLE);

                        Log.d("subTotal", "getTaxesfromRevalue" + cartHelper.getTaxesfromRevalue());
                    } else {
                        ToastUtils.showShortToastSafe(CartActivity.this, "on success body null");

                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                if (call.isCanceled()) {
                    ToastUtils.showShortToastSafe(CartActivity.this, "on cancel request");
                    getTotalPayableCappingAmount(isRemoveClicked, isAddClicked);
                } else {
                    ToastUtils.showShortToastSafe(CartActivity.this, "on fail");
                    mIsLoading = false;
                    mIsRemoveClicked = true;
                    mIsAddClicked = true;
                    amountPayableProgress.setVisibility(View.GONE);
                    totalTextView.setVisibility(View.VISIBLE);
                    placeOrderButton.setClickable(true);
                    placeOrderButton.setText("Confirm Order");
                    placeOrderButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                    totalTextView.setText("₹ " + (cartHelper.getTotalCustomerPayable() - cartHelper.getDiscount()));
                    cartHelper.setUserAmountPayable((int) (cartHelper.getTotalCustomerPayable() - cartHelper.getDiscount()));
                    totalStaticTextView.setText("Total :");
                    checkMinOrderAmount(cartHelper.getTotalCustomerPayable() + cartHelper.getDiscount());
                    taxesAndCapping.setVisibility(View.INVISIBLE);
                    taxesAndAdjustmentTextView.setVisibility(View.INVISIBLE);

                }
            }
        });

    } else {
        if (cappedAmountCall != null && !cappedAmountCall.isExecuted()) {
            mIsLoading = false;
            cappedAmountCall.cancel();
        }

    }

}
JakeWharton commented 7 years ago

I cannot run this code to verify the behavior. Can you create a standalone sample that reproduces it? Or a test case like the one linked in the issue template?

ankitjain10 commented 7 years ago

Thanks for early reply. It was my bad,all i need was to have instance declaration of call variable and callback...

On 17-Mar-2017 7:22 PM, "Jake Wharton" notifications@github.com wrote:

I cannot run this code to verify the behavior. Can you create a standalone sample that reproduces it? Or a test case like the one linked in the issue template?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/square/retrofit/issues/2252#issuecomment-287359079, or mute the thread https://github.com/notifications/unsubscribe-auth/AMGnWMhxaHXy2CMDOSzE8hCr1qJ_CaCHks5rmpAzgaJpZM4MgbaB .