鉴于任意凌空请求,如何捕获响应头和statusCode,无论成功/失败?
尝试/思路:
JsonResponseRequest
(gist,次要edit of this)getHeaders
的使用情况,但这似乎仅适用于请求GsonRequest
示例response.data
,标题和response.statusCode
的元组?:
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
return Response.success(response.statusCode,
HttpHeaderParser.parseCacheHeaders(response));
}
编辑:澄清一下,我有一个简单的tuple
类:
public final class ErrResStatusHeaders<E, R> {
public final E error;
public final R result;
public final int statusCode;
public final Map<String, String> headers;
在我的Activity
:
@Override
protected ErrResStatusHeaders<String,JSONObject> doInBackground(Void... params) {
// futures instantiated and utility function(s) called here
[...]
@Override
protected void onPostExecute(final ErrResStatusHeaders<String,JSONObject> er_res)
示例效用函数签名:
public ErrResStatusHeaders<String, JSONObject> register_or_login(
final HashMap<String, String> data,
final RequestFuture<JSONObject> register_future,
final RequestFuture<JSONObject> login_future) {
答案 0 :(得分:0)
String uri = "http://whatevergoeshere.com";
StringRequest stringRequest = new StringRequest("uri", responseListener, errorListener) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
Log.d("TEST", "Headers size:" + response.headers.size());
return super.parseNetworkResponse(response);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse != null && volleyError.networkResponse.headers != null)
Log.d("TEST", "Headers size:" + volleyError.networkResponse.headers.size());
return super.parseNetworkError(volleyError);
}
};
好吧,看起来这个问题发生了很大变化,所以这是我编辑过的答案:
您可以使用这样的自定义Request子类:
public class RawRequest extends Request<NetworkResponse> {
private final Response.Listener<NetworkResponse> mListener;
/**
* Creates a new GET request.
*
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public RawRequest(String url, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
/**
* Creates a new request with the given method.
*
* @param method the request {@link Method} to use
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public RawRequest(int method, String url, Response.Listener<NetworkResponse> listener,
Response.ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
return Response.success(response, HttpHeaderParser.parseCacheHeaders(response));
}
@Override
protected void deliverResponse(NetworkResponse response) {
mListener.onResponse(response);
}
}
然后在另一个班级中使用它:
String uri = "http://whatevergoeshere.com";
RequestFuture<NetworkResponse> future = RequestFuture.newFuture();
RawRequest rawRequest = new RawRequest("uri", future, future);
但这里的缺点显然是现在你必须自己解析整个回应。 可以通过编写自定义侦听器接口来避免这种情况,该接口传递已解析的响应(可以是JSON或String或image)以及标头作为第二个参数。然后你必须编写自己的RequestFuture类来支持它。
老实说,虽然我建议在AsyncTask之外尝试另一种方法。 Volley已经可以异步处理请求了。