我在项目中使用volley作为网络库。我需要使用session维护cookie。我从服务器获取cookie。那么我应该如何使用Volley并将其存储在sharedpreference中进行会话管理。
答案 0 :(得分:0)
//试试这个。
StringRequest req = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("response",response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("error",error.getMessage());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return getAuthHeader(context);
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
// since we don't know which of the two underlying network vehicles
// will Volley use, we have to handle and store session cookies manually
Log.i("response",response.headers.toString());
Map<String, String> responseHeaders = response.headers;
String cookies = responseHeaders.get("Set-Cookie");
Log.i("cookies",cookies);
return super.parseNetworkResponse(response);
}
};
答案 1 :(得分:0)
试试这个
在Application类中添加此代码,它将保存cookie。
{{1}}