我无法找到解决方法。截至目前,似乎我必须在获取正常的html响应或仅获取响应标头之间进行选择。
有没有办法修改我的代码以获得这两个?
当前代码(仅提供回复标题)
StringRequest stringRequest = new StringRequest(Request.Method.GET, loginURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String responseSession = response.substring(0,response.indexOf(";"));
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
}
}){
@Override
protected Response<String> parseNetworkResponse(NetworkResponse networkResponse) {
String sessionId = networkResponse.headers.get("Set-Cookie");
com.android.volley.Response<String> result = com.android.volley.Response.success(sessionId,
HttpHeaderParser.parseCacheHeaders(networkResponse));
return result;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
答案 0 :(得分:5)
为此目的使用自定义请求类
public class CustomStringRequest extends Request<CustomStringRequest.ResponseM> {
private Response.Listener<CustomStringRequest.ResponseM> mListener;
public CustomStringRequest(int method, String url, Response.Listener<CustomStringRequest.ResponseM> responseListener, Response.ErrorListener listener) {
super(method, url, listener);
this.mListener = responseListener;
}
@Override
protected void deliverResponse(ResponseM response) {
this.mListener.onResponse(response);
}
@Override
protected Response<ResponseM> 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<String, String> headers;
String response;
}
}
并改变这样的代码,
CustomStringRequest stringRequest = new CustomStringRequest(Request.Method.GET, loginURL,
new Response.Listener<CustomStringRequest.ResponseM>() {
@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();
}
}) {
};