当 error.networkResponse 为空时,Android Volley 中的 Http 状态代码 [英] Http Status Code in Android Volley when error.networkResponse is null
问题描述
我在 Android 平台上使用 Google Volley.我遇到了一个问题,其中 onErrorResponse
中的 error
参数返回一个 null networkResponse
对于我使用的 RESTful API,我需要确定 Http 状态代码,它通常以 401 (SC_UNAUTHORIZED) 或 500 (SC_INTERNAL_SERVER_ERROR) 的形式出现,我偶尔可以通过以下方式检查:
I am using Google Volley on the Android platform.
I am having a problem in which the error
parameter in onErrorResponse
is returning a null networkResponse
For the RESTful API I am using, I need to determine the Http Status Code which is often arriving as 401 (SC_UNAUTHORIZED) or 500 (SC_INTERNAL_SERVER_ERROR), and I can occasionally check via:
final int httpStatusCode = error.networkResponse.statusCode;
if(networkResponse == HttpStatus.SC_UNAUTHORIZED) {
// Http status code 401: Unauthorized.
}
这会抛出一个 NullPointerException
因为 networkResponse
为空.
This throws a NullPointerException
because networkResponse
is null.
如何判断onErrorResponse
函数中的Http状态码?
How can I determine the Http Status Code in the function onErrorResponse
?
或者,如何确保 error.networkResponse
在 onErrorResponse
中不为空?
Or, how can I ensure error.networkResponse
is non-null in onErrorResponse
?
推荐答案
401 Not Supported by Volley
事实证明,在不修改 Google Volley 代码的情况下无法保证 error.networkResponse 为非空,因为 Volley 中的一个错误会引发 Http 状态代码 401 的异常 NoConnectionError
(HttpStatus.SC_UNAUTHORIZED
) 在 BasicNetwork.java (134) 中设置 networkResponse
的值.
401 Not Supported by Volley
It turns out that it is impossible to guarantee that error.networkResponse is non-null without modifying Google Volley code because of a bug in Volley that throws the Exception NoConnectionError
for Http Status Code 401 (HttpStatus.SC_UNAUTHORIZED
) in BasicNetwork.java (134) prior to setting the value of networkResponse
.
在这种情况下,我们的解决方案不是修复 Volley 代码,而是修改 Web 服务 API 以针对相关特定情况发送 Http 错误代码 403 (HttpStatus.SC_FORBIDDEN
).
Instead of fixing the Volley code, our solution in this case was to modify the Web Service API to send Http Error Code 403 (HttpStatus.SC_FORBIDDEN
) for the particular case in question.
对于这个 Http 状态码,error.networkResponse
的值在 Volley 错误处理程序中是非空的:public void onErrorResponse(VolleyError error)
.并且,error.networkResponse.httpStatusCode
正确返回 HttpStatus.SC_FORBIDDEN
.
For this Http Status Code, the value of error.networkResponse
is non-null in the Volley error handler: public void onErrorResponse(VolleyError error)
. And, error.networkResponse.httpStatusCode
correctly returns HttpStatus.SC_FORBIDDEN
.
Rperryng 提出的扩展 Request
类的建议可能已经提供了一个解决方案,并且是一个创造性和极好的想法.非常感谢您提供的详细示例.我发现我们案例的最佳解决方案是使用变通方法,因为我们很幸运能够控制 Web 服务 API.
Rperryng's suggestion of extending the Request<T>
class may have provided a solution, and is a creative and excellent idea. Thank you very much for the detailed example. I found the optimal solution for our case is to use the work-around because we are fortunate enough to have control of the web services API.
如果我无法在服务器上进行简单的更改,我可能会选择在 BasicNetwork.java 中的一个位置修复 Volley 代码.
I might opt for fixing the Volley code in one location within BasicNetwork.java if I did not have access to making a simple change at the server.
这篇关于当 error.networkResponse 为空时,Android Volley 中的 Http 状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!