说明:
Volley框架适合进行小数据、多频发的网络操作,以及图片的加载,然而有个缺点:对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕。
今天我就给大家带来volley框架获取cookie和带cookie的请求教程。
Android端模拟浏览器访问的思路:
1、通过框架获取第一次登陆成功访问返回的Cookie
2、用SharedPreferences保存Cookie
3、每次请求都将保存的Cookie放在请求头部
代码:
Cookie:
一、请求获取Cookie:
RequestQueue mQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.url+"/port/login_port.ashx", new Response.Listener<String>() {
@Override
public void onResponse(String s) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
return map;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
try {
Map<String, String> responseHeaders = response.headers;
String rawCookies = responseHeaders.get("Set-Cookie");
saveSettingNote(Config.Cookie,rawCookies);//保存Cookie-saveSettingNote是本地存储
Log.i("px",rawCookies+"\n");
String dataString = new String(response.data, "UTF-8");
Log.i("px",dataString);
return Response.success(dataString, HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
};
mQueue.add(stringRequest);
二、带Cookie的请求
RequestQueue mQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.url+"/port/get_category_port.ashx", new Response.Listener<String>() {
@Override
public void onResponse(String s) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap localHashMap = new HashMap();
localHashMap.put("Cookie", getSettingNote(Config.Cookie));//向请求头部添加Cookie-本地得到cookie
return localHashMap;
}
};
mQueue.add(stringRequest);
保存cookie:
一、保存Cookie的方法:
private void saveSettingNote(String s,String saveData){//保存设置
SharedPreferences.Editor note = getSharedPreferences(Config.ShareTo,MODE_PRIVATE).edit();
note.putString(s, saveData);
note.commit();
}
二、从记录中获取Cookie的方法:
private String getSettingNote(String s){//获取保存设置
SharedPreferences read = getSharedPreferences(Config.ShareTo, MODE_PRIVATE);
return read.getString(s, "");
}
Params:
private void stringRequestWithPost(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//使用JSONObject给response转换编码
JSONObject jsonObject = new JSONObject(response);
responseText.setText(jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
responseText.setText(error.getMessage());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<>();
map.put("cityname","朝阳");
return map;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> map = new HashMap<>();
map.put("apikey","f71e5f1e08cd5a7e42a7e9aa70d22458");
return map;
}
};
mQueue.add(stringRequest);
}