如何在 Volley onErrorResponse 中获取 JSON 错误响应

编程入门 行业动态 更新时间:2024-10-27 03:37:46
本文介绍了如何在 Volley onErrorResponse 中获取 JSON 错误响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 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 != errorworkResponse)
        {
            Log.d(TAG + ": ", "Error Response code: " + errorworkResponse.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 = errorworkResponse;
 if (networkResponse != null && networkResponse.data != null) {
       String jsonError = new String(networkResponse.data);
       // Print Error!
 }

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

更多推荐

[db:关键词]

本文发布于:2023-04-14 23:47:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/851426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   如何在   onErrorResponse   Volley   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!