Java 如何从 VolleyError 中获取响应代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22347993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the response code from a VolleyError?
提问by Bart Burg
I'm looking for a way to get the response code of a thrown VolleyError. My ErrorListener looks like this:
我正在寻找一种方法来获取抛出的 VolleyError 的响应代码。我的 ErrorListener 看起来像这样:
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//Get response code here
VolleyLog.e("Error: ", error.toString());
VolleyLog.e("Error: ", error.getLocalizedMessage());
}
};
The 2 lines I send to my VolleyLog print the following:
我发送到我的 VolleyLog 的 2 行打印如下:
03-12 10:57:56.932: E/Volley(7147): [1] 1.onErrorResponse: Error:
03-12 10:57:56.932: E/Volley(7147): [1] 1.onErrorResponse: Error:
Volley does know what comes back because I can also see the following in my VolleyLog:
Volley 确实知道返回什么,因为我还可以在我的 VolleyLog 中看到以下内容:
03-12 10:57:56.692: E/Volley(7147): [41854] BasicNetwork.performRequest: Unexpected response code 403 for https://*******/Employee/authenticate
03-12 10:57:56.897: E/Volley(7147): [41854] BasicNetwork.performRequest: Unexpected response code 403 for https://*******/Employee/authenticate
03-12 10:57:56.902: W/System.err(7147): com.android.volley.AuthFailureError
03-12 10:57:56.902: W/System.err(7147): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:143)
03-12 10:57:56.902: W/System.err(7147): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
So basically, what I want is to get that response code "403" (Forbidden).
所以基本上,我想要的是获得响应代码“403”(禁止)。
采纳答案by Blackbelt
through the VolleyError:
通过 VolleyError:
error.networkResponse.statusCode
looking at the source code, I saw that VolleyError has a public final member NetworkResponse
called networkResponse
, that holds that statusCode
of the Http call. You should check for NPE.
查看源代码,我看到 VolleyError 有一个NetworkResponse
名为的公共 final 成员networkResponse
,它保存statusCode
了 Http 调用的成员。您应该检查 NPE。
Hereyou can find the source code for the NetworkResponse
.
Hereyou can find the source code for the VolleyError