square / retrofit

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

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

Closed ankitjain10 closed 7 years ago

ankitjain10 commented 7 years ago

What kind of issue is this?

JakeWharton commented 7 years ago

Please provide a failing test case.

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();
        }

    }

}