如何在 Volley onErrorResponse 中获取 JSON 错误响应 [英] How to get JSON error response in Volley onErrorResponse

查看:34
本文介绍了如何在 Volley onErrorResponse 中获取 JSON 错误响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 API 调用,其中包含以下详细信息

I have an API call with below details

URL:http://pankajservers.in/api/v1/AuthenticateUser 带输入参数下面

URL: http://pankajservers.in/api/v1/AuthenticateUser with Input params below

电子邮件地址和密码

如果我输入错误的凭据,我会收到以下带有状态代码的 JSON 响应:403

if I type wrong credentials, I get below JSON response with Status Code : 403

{"Status":false,"Message":"Invalid Credentials. 5 attempts left.","Data":null}

以下是错误响应代码.

new Response.ErrorListener()
{
    @Override
    public void onErrorResponse(VolleyError error)
    {
        if (null != error.networkResponse)
        {
            Log.d(TAG + ": ", "Error Response code: " + error.networkResponse.statusCode);
        }
    }
});

有没有办法在这个代码块中获得 JSON 响应?

Is there any way to get JSON response in this code block?

我试过这篇文章:Volley handle onErrorResponse

但它似乎永远不会执行 parseNetworkError

But it seems it never execute parseNetworkError

推荐答案

要在该块上打印错误,您只需要获取响应字节:

To print the Error on that block you just need to obtain the Response bytes:

 NetworkResponse networkResponse = error.networkResponse;
 if (networkResponse != null && networkResponse.data != null) {
       String jsonError = new String(networkResponse.data);
       // Print Error!
 }

这篇关于如何在 Volley onErrorResponse 中获取 JSON 错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆