如何在android中使用retrofit访问404错误?

2024-05-13

我正在使用改造 2 访问 REST API 以使用原始正文插入 JSON 数据,我从服务器获得成功响应,但在响应时收到 404 错误

我想访问404错误请帮我解决这个问题

ApiUtil.getServiceClass().sendFinalCart(new ConfirmCartData(_id, sharedPrefManager.getUserID(), grandtotal,
    grandquantity, DDK, payableprice, redeemQuantityModels)).enqueue(new Callback < CardAddToCart > () {
    @Override
    public void onResponse(Call < CardAddToCart > call, Response < CardAddToCart > response) {
        if (response.isSuccessful()) {
            cardAddToCart = response.body();

            float result = cardAddToCart.getCartSuccess().getBalance();
            float payablePrice = cardAddToCart.getCartSuccess().getPayablePrice();
            float required = cardAddToCart.getCartSuccess().getRequired();


        } else {
            if (response.code() == 404) {
                cardAddToCart = response.body();

                try {
                    success = response.errorBody().string();
                    Toast.makeText(RedeemCartScreen.this, "" + success, Toast.LENGTH_SHORT).show();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            success = cardAddToCart.getSuccess();
        }
    }

    @Override
    public void onFailure(Call < CardAddToCart > call, Throwable t) {

    }
});    

我对 404 错误的响应是这样的。

{
"success": "not enough balance, pleace recharge ddk wallet",
"result": {
    "balance": 1,
    "payablePrice": 85,
    "amountSaved": null,
    "required": -14.555
}

}

成功响应 (200)。

{
  "success": "transection successfull",
  "result": {
    "balance": 1,
    "payablePrice": 100,
    "amountSaved": 0,
    "required": ""
  }
}

您可以从后端团队获取 json 中的代码,然后可以将结果与代码进行比较,并可以在后端团队发送的代码的帮助下完成您的工作 :-

在您的余额不足的状态下:-

{
"success": "not enough balance, pleace recharge ddk wallet",
"code": 200,
"isBalance": false,  //it means you dont have enough balance
"result": {
    "balance": 1,
    "payablePrice": 85,
    "amountSaved": null,
    "required": -14.555
}

在您的成功案例中:-

{
  "success": "transection successfull",
  "code": 200,
  "isBalance": true, // it means you have sufficient balance
  "result": {
    "balance": 1,
    "payablePrice": 100,
    "amountSaved": 0,
    "required": ""
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android中使用retrofit访问404错误? 的相关文章

随机推荐