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

问题描述

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

I have an API call with below details

电子邮件地址和密码

如果我输入了错误的凭据,则会收到状态码为403的JSON响应

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?

我尝试了这篇文章:凌空句柄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错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:20