自带的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 AND EXPIRY_DATE != 0",new String[] { String.valueOf(time) });}private static class Column {public static final String AUTO_ID = "AUTO_ID";public static final String VALUE = "VALUE";public static final String NAME = "NAME";public static final String COMMENT = "COMMENT";public static final String DOMAIN = "DOMAIN";public static final String EXPIRY_DATE = "EXPIRY_DATE";public static final String PATH = "PATH";public static final String SECURE = "SECURE";public static final String VERSION = "VERSION";}
}
3、把COOKIEStore设置到httpclient
httpClient.setCOOKIEStore(new CustomCOOKIEStore());