为此目的使用自定义请求类,
public class CustomStringRequest extends Request{ private Response.Listener mListener; public CustomStringRequest(int method, String url, Response.Listener responseListener, Response.ErrorListener listener) { super(method, url, listener); this.mListener = responseListener; } @Override protected void deliverResponse(ResponseM response) { this.mListener.onResponse(response); } @Override protected Response parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } ResponseM responseM = new ResponseM(); responseM.headers = response.headers; responseM.response = parsed; return Response.success(responseM, HttpHeaderParser.parseCacheHeaders(response)); } public static class ResponseM { Map headers; String response; } }
并改变这样的代码,
CustomStringRequest stringRequest = new CustomStringRequest(Request.Method.GET, loginURL, new Response.Listener() { @Override public void onResponse(CustomStringRequest.ResponseM result) { //From here you will get headers String sessionId = result.headers.get("Set-Cookie"); String responseString = result.response; } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG ).show(); } }) { };