本文介绍了如何获取状态code在成功响应排球的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问一下如何使用凌空时获取状态code。
I would like to ask how to get status code when using Volley.
我有以下code:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Here I want to get status code
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
)
这可能吗?如果不是,为什么?
Is it possible ? If not, why ?
推荐答案
也许这个类,而不是StringResponse:
Maybe this class instead of StringResponse:
public class NetworkResponseRequest extends Request<NetworkResponse> {
private final Response.Listener<NetworkResponse> mListener;
public NetworkResponseRequest(int method, String url, Response.Listener<NetworkResponse> listener,
Response.ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
public NetworkResponseRequest(String url, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
@Override
protected void deliverResponse(NetworkResponse response) {
mListener.onResponse(response);
}
@Override
protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {
return Response.success(response, HttpHeaderParser.parseCacheHeaders(response));
}
public static String parseToString(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return parsed;
}
}
用法:
NetworkResponseRequest request = new NetworkResponseRequest(Request.Method.GET, "http://example.com/test.php",
new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
// This is status code: response.statusCode
// This is string response: NetworkResponseRequest.parseToString(response)
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
这是不是很好用我自己的实现,而不是StringRequest。你对此怎么看?
It is not nice to use my own implementation instead of StringRequest. What do you think about it ?
这篇关于如何获取状态code在成功响应排球的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!