Volley框架設置sessionid,volleysessionid
(偷懶,寫簡略點)
自定義一個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)