(偷懒,写简略点)
自定义一个Request类
public class MyRequest extends Request<JSONObject>
存储上一次连接的sessionid
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try
{
Map<String, String> responseHeaders = response.headers;
Log.e("MyRequest", "response:" + responseHeaders.toString());
String rawCookies = responseHeaders.get("Set-Cookie");
SharedPreferencesUtil.putString("sessionid",rawCookies.substring(0, rawCookies.indexOf(";")));
Log.e("MyRequest", "parseNetworkResponse() sessionId:" + sessionId);
}
下一次连接报文头加上sessionid
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
sessionId=SharedPreferencesUtil.getString("sessionid");
// Log.e("MyRequest", "getHeaders() sessionId:" + sessionId);
if (!TextUtils.isEmpty(sessionId)) {
headers.put("cookie", "" + sessionId);
}
return headers;
}
如:获取验证码注册(获取验证码是返还一个sessionid,注册时加上该sessionid)