Volley是很好用的網絡請求框架,可是有時候咱們須要用到會話的管理,而Volley是不支持cookie的,不過慶幸的是Volley的擴展性很強,因此咱們能夠重寫一些方法來達到咱們想要的效果,例如登錄的時候,想提交cookie給服務器,並從服務器讀取cookie等json
咱們在請求網絡的時候,他會返回一個response,在這個response中有一個headers就包含了cookie的字段,切記的是,咱們若是要存cookie的話,鍵值對的鍵必須是固定的"Cookie" ,不然識別不出來;調用headers返回的cookie以下: 服務器
因此咱們若是要用的話,須要用正則來匹配這個字符串cookie
如下是貼出的代碼:
網絡
public class GsonRequest<T> extends Request<T> {
private final Gson mGson;
private final Class<T> mClass;
private final Listener<T> mListener;
private Map<String, String> mMap;
private Map<String, String> sendHeader = new HashMap<String, String>(1);
private String mHeader;
private String cookieFromResponse = "";
public GsonRequest(int method, String url, Class<T> objectClass, Listener<T> listener, ErrorListener errorListener) {
super(method, url, errorListener);
this.mClass = objectClass;
this.mListener = listener;
mGson = new Gson();
}
/**
* 用於POST請求的構造函數
*
* @param url
* 地址
* @param map
* 參數
* @param objectClass
* 輸出類型
* @param listener
* 正確返回結果的監聽
* @param errorListener
* 錯誤返回結果的監聽
*/
public GsonRequest(String url, Map<String, String> map, Class<T> objectClass, Listener<T> listener, ErrorListener errorListener) {
super(Request.Method.POST, url, errorListener);
this.mClass = objectClass;
this.mListener = listener;
this.mMap = map;
mGson = new Gson();
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return mMap;
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data, "UTF-8");框架
//這裏就是返回的Set-cookie的cookie字符串
ide
mHeader = response.headers.toString();
Pattern pattern = Pattern.compile("Set-Cookie.*?;");
Matcher m = pattern.matcher(mHeader);
if (m.find()) {
cookieFromResponse = m.group();
}
if (cookieFromResponse.length() > 12) {
// 去掉cookie末尾的分號
cookieFromResponse = cookieFromResponse.substring(11, cookieFromResponse.length() - 1);
}
JSONObject jsonObject = new JSONObject(json);函數
//這裏是本身的處理,你們能夠根據本身的需求來進行相應的改動
this
if(jsonObject.has("SmsBean")){
jsonObject.getJSONObject("SmsBean").put("Cookie", cookieFromResponse);
}
if(jsonObject.has("ResultMsg"))
{
jsonObject.getJSONObject("ResultMsg").put("Cookie", cookieFromResponse);
}
return Response.success(mGson.fromJson(jsonObject.toString(), mClass), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return sendHeader;
}
public void setSendCookie(String cookie) {url
sendHeader.put("Cookie", cookie); //這裏的鍵必須是Cookie
}
}.net
若是咱們要是使用的話,能夠以下,這是本身封裝的一個方法,格式以下
public class RequestUtil{
public static void sendMsg(String cookie,String name, String number, Listener<SendSmsBean> listener, ErrorListener errorListener) {
Map<String, Object> sendMesParams = new HashMap<String, Object>();
sendMesParams.put("name", name);
sendMesParams.put("number", number);
String url = "www.xxxx.com";
GsonRequest<SendSmsBean> request = new GsonRequest<XXXBean>(Method.GET, url, XXXBean.class, listener, errorListener);
request.setSendCookie(cookie);
RequestManager.getRequestQueue().add(request);
}
}