热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

androidhttpclient管理cookie

自带的CookieStore只能在内存里边管理cookie,但是有的时候需要把cookie保存到文件里边。为了长久保存cookie,自定义了BasicC
自带的COOKIEStore只能在内存里边管理COOKIE,但是有的时候需要把COOKIE保存到文件里边。

为了长久保存COOKIE,自定义了BasicCOOKIEStore,当添加COOKIE的时候,都保存到数据库,初始化的时候再从数据里边恢复COOKIE。

1、自定义的COOKIEStore

public class CustomCOOKIEStore extends BasicCOOKIEStore {public CustomCOOKIEStore() {super();try {List COOKIEs = COOKIEDBManager.getInstance().getAllCOOKIEs();COOKIEDBManager.getInstance().clear();COOKIE[] COOKIEsArr = new COOKIE[COOKIEs.size()];for (int i = COOKIEs.size() - 1; i >= 0; i--) {COOKIEsArr[i] = COOKIEs.get(i);}addCOOKIEs(COOKIEsArr);} catch (Exception e) {e.printStackTrace();}}@Overridepublic synchronized void addCOOKIE(COOKIE COOKIE) {super.addCOOKIE(COOKIE);try {COOKIEDBManager.getInstance().saveCOOKIE(COOKIE);} catch (Exception e) {e.printStackTrace();}}@Overridepublic synchronized void addCOOKIEs(COOKIE[] COOKIEs) {super.addCOOKIEs(COOKIEs);try {COOKIEDBManager.getInstance().saveCOOKIEs(COOKIEs);} catch (Exception e) {e.printStackTrace();}}@Overridepublic synchronized void clear() {super.clear();try {COOKIEDBManager.getInstance().clear();} catch (Exception e) {e.printStackTrace();}}@Overridepublic synchronized boolean clearExpired(Date date) {try {COOKIEDBManager.getInstance().clearExpired();} catch (Exception e) {e.printStackTrace();}return super.clearExpired(date);}@Overridepublic synchronized List getCOOKIEs() {return super.getCOOKIEs();}@Overridepublic String toString() {return super.toString();}}


2、数据库

public class COOKIEDBManager {private final String DB_NAME = "COOKIE.db";private final String TABLE_NAME = "COOKIE";private DBHelper dbHelper;private SQLiteDatabase db;private static COOKIEDBManager instance;public static COOKIEDBManager getInstance() {if (instance == null) {instance = new COOKIEDBManager();}return instance;}public COOKIEDBManager() {dbHelper = new DBHelper(App.getCurrentApp(), DB_NAME, null, 1);db = dbHelper.getWritableDatabase();}private class DBHelper extends SQLiteOpenHelper {private String SQL_CAREATE_DB = "CREATE TABLE IF NOT EXISTS "//+ TABLE_NAME + " (" + //Column.AUTO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + //Column.VALUE + " TEXT," + //Column.NAME + " TEXT," + //Column.COMMENT + " TEXT," + //Column.DOMAIN + " TEXT," + //Column.EXPIRY_DATE + " INTEGER," + //Column.PATH + " TEXT," + //Column.SECURE + " INTEGER," + //Column.VERSION + " TEXT)";//public DBHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(SQL_CAREATE_DB);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);db.execSQL(SQL_CAREATE_DB);}}public List getAllCOOKIEs() {List COOKIEs = new ArrayList();Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {String name = cursor.getString(cursor.getColumnIndex(Column.NAME));String value = cursor.getString(cursor.getColumnIndex(Column.VALUE));BasicClientCOOKIE COOKIE = new BasicClientCOOKIE(name, value);COOKIE.setComment(cursor.getString(cursor.getColumnIndex(Column.COMMENT)));COOKIE.setDomain(cursor.getString(cursor.getColumnIndex(Column.DOMAIN)));long expireTime = cursor.getLong(cursor.getColumnIndex(Column.EXPIRY_DATE));if (expireTime != 0) {COOKIE.setExpiryDate(new Date(expireTime));}COOKIE.setPath(cursor.getString(cursor.getColumnIndex(Column.PATH)));COOKIE.setSecure(cursor.getInt(cursor.getColumnIndex(Column.SECURE)) == 1);COOKIE.setVersion(cursor.getInt(cursor.getColumnIndex(Column.VERSION)));COOKIEs.add(COOKIE);}cursor.close();return COOKIEs;}public void saveCOOKIE(COOKIE COOKIE) {LogUtil.d("saveCOOKIE:" + COOKIE);if (COOKIE == null) {return;}db.delete(TABLE_NAME, Column.NAME + " = ? ",new String[] { COOKIE.getName() });ContentValues values = new ContentValues();values.put(Column.VALUE, COOKIE.getValue());values.put(Column.NAME, COOKIE.getName());values.put(Column.COMMENT, COOKIE.getComment());values.put(Column.DOMAIN, COOKIE.getDomain());if (COOKIE.getExpiryDate() != null) {values.put(Column.EXPIRY_DATE, COOKIE.getExpiryDate().getTime());}values.put(Column.PATH, COOKIE.getPath());values.put(Column.SECURE, COOKIE.isSecure() ? 1 : 0);values.put(Column.VERSION, COOKIE.getVersion());db.insert(TABLE_NAME, null, values);}public void saveCOOKIEs(COOKIE[] COOKIEs) {LogUtil.d("saveCOOKIEs:" + COOKIEs);if (COOKIEs == null) {return;}db.beginTransaction();for (COOKIE COOKIE : COOKIEs) {saveCOOKIE(COOKIE);}db.setTransactionSuccessful();db.endTransaction();}public void clear() {db.delete(TABLE_NAME, null, null);}public void clearExpired() {long time = System.currentTimeMillis();db.delete(TABLE_NAME, "EXPIRY_DATE }


3、把COOKIEStore设置到httpclient

httpClient.setCOOKIEStore(new CustomCOOKIEStore());




转:https://www.cnblogs.com/bwlcool/p/8575366.html



推荐阅读
author-avatar
梦的影子2502931765
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有