如何在Android中使用翻新来访问404错误?

时间:2019-05-17 05:35:52

标签: java android retrofit2

我正在使用翻新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": ""
  }
}

1 个答案:

答案 0 :(得分:0)

您可以从后端团队获取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": ""
  }
}