android DefaultHttpClient设置setCookieStore
2015-06-25 10:15
756 查看
android DefaultHttpClient设置setCookieStore
第一步:设置CookieStoreUtils的工具类import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.http.client.CookieStore; import org.apache.http.cookie.Cookie; import org.apache.http.impl.cookie.BasicClientCookie; import android.app.Activity; import android.content.SharedPreferences; import com.google.gson.Gson; public class CookieStoreUtils { @SuppressWarnings("unchecked") public static CookieStore initCookieStore(CookieStore cookieStore){ SharedPreferences mySharedPreferences = MyApplication.mInstance.getSharedPreferences( "cookies", Activity.MODE_PRIVATE); Map<String, String> map = (Map<String, String>) mySharedPreferences.getAll(); if (map != null) { Iterator<String> it = map.keySet().iterator(); Gson gson = new Gson(); while (it.hasNext()) { String key = it.next(); String jsonCookie = map.get(key); if (jsonCookie != null) { BasicClientCookie cookie = gson.fromJson(jsonCookie, BasicClientCookie.class); cookieStore.addCookie(cookie); } } } return cookieStore; } public static void saveCookies(List<Cookie> cookies){ SharedPreferences mySharedPreferences = MyApplication.mInstance.getSharedPreferences("cookies", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = mySharedPreferences.edit(); Gson gson = new Gson(); for(Cookie cookie:cookies){ editor.putString(cookie.getName(), gson.toJson(cookie));// JSON转换成String类型 } editor.commit(); } }
第二步:
private static CookieStore cookieStore = null;
/* * POST方式,查询从URL得到的字符串 */ public static String queryStringForPost(String url, List<NameValuePair> params) { DefaultHttpClient client = new DefaultHttpClient(); if(cookieStore==null){ cookieStore = CookieStoreUtils.initCookieStore(client.getCookieStore()); } client.setCookieStore(cookieStore); // 获取HttpPost对象 HttpPost httpPost = new HttpPost(url); String result = "isError"; HttpResponse httpResponse = null; try { // 设置HttpPost请求参数 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = client.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toString(httpResponse.getEntity()); if (result.equals("{\"data\":null}") || result.equals("{\"data\":[]}")) {// 如果后台数据为空 return "isNull";// 返回为空的结果 } } //保存cookie到本地 CookieStoreUtils.saveCookies(cookieStore.getCookies()); return result; } catch (Exception e) { e.printStackTrace(); } return result; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories