之前项目里请求网络的代码都是采用原生的语句,如HttpURLConnection或者HttpClient,这种方式请求灵活度高,但是访问网络的速度慢,特别是在网络不好的情况下情况更糟,
而Volley是google官方极力推崇的一款网络请求框架,具体好处自不用多说.且因为它的继承性,在灵活度上也比原生的请求代码好一些,我们可以继承父类Request
当采用post方式请求数据时,我们一般都要添加请求参数,和一些header信息,cookie信息自然也不例外, 而cookie的作用主要就是保存用户的一些身份信息等,如一个软件在使用的过程.从登陆账号成功的那一刻起,到关闭软件退出的那一刻结束,这段时间内在软件的任何有关网络的post请求操作都要带上cookie信息而cookie信息是服务器端返回的,所以软件在第一次登陆的时候肯定是没有cookie的,当登陆成功后,服务器端返回一段保存在"Set-Cookie"头字段里的cookie信息,客户端拿到这段数据以后,一般都要对返回的Cookie进行抽取,把真正的cookie信息抽取出来然后保存在sharedPreference里或者数据库里,然后在软件的使用过程中,所有需要cookie的地方都从sharedPreference里或者数据库里取,但是cookie在服务端的存储也是有时间限制的,所以以后的每次登陆都要在登陆成功后把最新的cookie信息保存一遍,这样能保证每次用的cookie都是有效的.
Volley框架中有获取cookie的方式,只要重写parseNetWorkResponse,该回调方法返回一个NetWorkResponse对象,而NetWorkResponse.headers方法就可以获取到所有的响应头字段,所有的头字段封装在Map集合里,如下代码:
Map headers = response.headers;//获取所有头字段
String cookies = headers.get("Set-Cookie");//获取Cookie头字段
String data = new String(response.data, "UTF-8");//获取服务器返回的数据
大家也看到了,通过header.get("Set-Cookie")方法获取服务器端返回的cookie信息,但是有个不足,Volley默认只返回第一条Cookie信息,如果服务端返回了多条cookie信息,而我们只能获取一条,这显然不行,所以我们需要修改源码,之前也在网络上找了一些资料,但是要么就是说继承Request自己实现的,要么就是自己修改源码但是都没说明白的,但是继承Request自己实现这种方式还是解决不了获取所有cookie信息的问题,因为只要是继承request,都需要重写两个方法,一个deliverResponse(String response),方法,该方法一般只回调mListener.onResponse(response)即可,另一个必须要重写的就是parseNetworkResponse(NetworkResponse response)方法,该方法就是解析NetworkResponse ,并把解析好的结果返回给deliverResponse(String response),而解析好的结果一般也就是我们需要展示到界面上的东西,但是我们解析的NetworkResponse 是Volley在添加完头字段后返回的,也就是说,Volley已经把cookie信息的第一条数据取出来后,才返回的NetworkResponse ,所以即使继承request自己实现,还是只会获取到1条cookie信息,并不能获取所有的cookie,如下Volley源码,具体位置在com.android.volley.toolbox.HurlStack类中的performRequest方法中可以看到
@Override
public HttpResponse performRequest(Request> request, Map additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap map = new HashMap();
if (mUserAgent != null) {
map.put("User-Agent", mUserAgent);
}
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
//////////////////////////////////////////////////////////////////
(Entry> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
////////////看这里~~/////////////////////////
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
里,这样在调用parseNetworkResponse(NetworkResponse response)方法的时候,NetworkResponse里就存储了所有的cookie信息,我们直接获取即可.具体的代码如下
首先是修改获取value的地方:
@Override
public HttpResponse performRequest(Request> request, Map additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap map = new HashMap();
if (mUserAgent != null) {
map.put("User-Agent", mUserAgent);
}
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion,
connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry> header : connection.getHeaderFields().entrySet()) {
///////////改成如下代码////////////////////////
if (header.getKey() != null) {
StringBuilder builder = new StringBuilder();
//获取一个key中的所有value
for(int i=0;i
@Override
protected Response parseNetworkResponse(
NetworkResponse response) {
try {
Map headers = response.headers;
String cookies = headers.get("Set-Cookie");
String data = new String(response.data, "UTF-8");
// 解析服务器返回的cookie值
String cookie = parseVolleyCookie(cookies);
// 存储cookie
SharedPreferenceUtil.putString(LoginActivity.this,"cookie", cookie);
// 下面这句是把返回的数据传到onResponse回调里,一定要用这种方式写
return Response.success(data,HttpHeaderParser.parseCacheHeaders(response));
}
catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
cookieEmployeeId=40288186519ea85901519ead34c50001; Path=/cookieSysUserId=40288186519ea85901519ead34ae0000; Path=/JSESSIONID=B6200ABA230422C2A0D434A2645FB400; Path=/wash-web/; HttpOnly
cookieSysUserId=40288186519ea85901519ead34ae0000;JSESSIONID=B6200ABA230422C2A0D434A2645FB400;cookieEmployeeId=40288186519ea85901519ead34c50001;
没有Path=/,也没有什么/wash-web/; HttpOnly之类的东西才对,,所以我们下面需要做的就是解析这个cookie,把我们想要的数据抽取出来组合成我们理想的格式,可能有人问为什么会有path/之类的东西,产生这种情况的主要原因是因为服务器端程序在被访问的时候,会根据你的身份开放相应的功能,比如A功能是放在a路径下,产生的cookie是a路径下的,B功能放在b路径下,产生的cookie是b路径下的,所以返回的总cookie就有path/等问题,产生的原因说明完毕,我们就开始解析,解析也是具有针对性的,应该有通用的解析方法,不过目前还没想好,如下代码:
/*
* 方法的作用: 解析volley返回cookie
*/
public String parseVooleyCookie(String cookie) {
StringBuilder sb = new StringBuilder();
String[] cookievalues = cookie.split(";");
for (int j = 0; j < cookievalues.length; j++) {
String[] keyPair = cookievalues[j].split("/");
for (int i = 0; i < keyPair.length; i++) {
if (keyPair.length == 2) {
if (keyPair[1].contains("cookieSysUserId")
|| keyPair[1].contains("JSESSIONID")) {
sb.append(keyPair[1]);
sb.append(";");
break;
}
} else {
if (keyPair[0].contains("cookieEmployeeId")) {
sb.append(keyPair[0]);
sb.append(";");
break;
}
}
}
}
return sb.toString();
}
解析之后就和我们理想的cookie格式一样了:
cookieSysUserId=40288186519ea85901519ead34ae0000;JSESSIONID=B6200ABA230422C2A0D434A2645FB400;cookieEmployeeId=40288186519ea85901519ead34c50001;
@Override
public Map getHeaders() throws AuthFailureError {
// 给服务器上传cookie值
Map map = new HashMap();
String cookie = SharedPreferenceUtil.getString(
LoginActivity.this, "cookie");
map.put("cookie", cookie);
return map;
}
下面贴出完整的以StringRequest为例的请求操作代码:
private void getLoginFromServer(final Map params) {
StringRequest request = new StringRequest(Method.POST,
NetAccessAddress.getLoginUrlAction(), new Listener() {
@Override
public void onResponse(String response) {
if (dialog != null && dialog.isShowing())
dialog.dismiss();
parseResult(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (dialog != null && dialog.isShowing())
dialog.dismiss();
Toast.makeText(getApplicationContext(),
"服务器连接异常!" , Toast.LENGTH_SHORT)
.show();
}
}) {
@Override
protected Map getParams() throws AuthFailureError {
// 添加post请求参数
return params;
}
@Override
public Map getHeaders() throws AuthFailureError {
// 添加头部信息,给服务器上传cookie值
Map map = new HashMap();
String cookie = SharedPreferenceUtil.getString(
LoginActivity.this, "cookie");
map.put("cookie", cookie);
return map;
}
@Override
protected Response parseNetworkResponse(
NetworkResponse response) {
try {
Map headers = response.headers;
String cookies = headers.get("Set-Cookie");
String data = new String(response.data, "UTF-8");
// 解析服务器返回的cookie值
String cookie = parseVooleyCookie(cookies);
// 存储cookie
SharedPreferenceUtil.putString(LoginActivity.this,
"cookie", cookie);
// 下面这句是把返回的数据传到onResponse回调里
return Response.success(data,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
};
MyApplication.getQueue().add(request);//添加请求到队列里
}
Volley的library文件,和volley.jar都以一样的效果,只不过library文件修改起来不管是使用AndroidStudio还是eclipse都特别方便,并且关于获取多条Cookie的代码我已经在这个library里改好了,其他的都和官方的一模一样.
下载地址如下:http://download.csdn.net/detail/qiang_xi/9422609