androidcookie存储sqllite

/**声明一些数据库操作的常量*/
  private static SQLiteDatabase mDatabase = null;
  private static final String DATABASE_FILE = "webview.db";
  private static final String COOKIES_NAME_COL = "name";
  private static final String COOKIES_VALUE_COL = "value";
  private static final String COOKIES_DOMAIN_COL = "domain";
  private static final String COOKIES_PATH_COL = "path";
  private static final String COOKIES_EXPIRES_COL = "expires";
  private static final String COOKIES_SECURE_COL = "secure";
mDatabase = LoginApiActivity.this.openOrCreateDatabase(DATABASE_FILE, 0, null);
//创建cookie数据库
    if (mDatabase != null) {
      // cookies
      mDatabase.execSQL("CREATE TABLE IF NOT EXISTS cookies "
              + " (_id INTEGER PRIMARY KEY, "
              + COOKIES_NAME_COL + " TEXT, " + COOKIES_VALUE_COL
              + " TEXT, " + COOKIES_DOMAIN_COL + " TEXT, "
              + COOKIES_PATH_COL + " TEXT, " + COOKIES_EXPIRES_COL
              + " INTEGER, " + COOKIES_SECURE_COL + " INTEGER" + ");");
      mDatabase.execSQL("CREATE INDEX IF NOT EXISTS cookiesIndex ON "
              + "cookies" + " (path)");
    }
  }
 
/*写cookie*/
  public void addCookie(Cookie cookie) {
    if (cookie.getDomain() == null || cookie.getPath() == null || cookie.getName() == null
            || mDatabase == null) {
        return;
    }
    String mCookieLock = "asd";
    synchronized (mCookieLock) {
        ContentValues cookieVal = new ContentValues();
        cookieVal.put(COOKIES_DOMAIN_COL, cookie.getDomain());
        cookieVal.put(COOKIES_PATH_COL, cookie.getPath());
        cookieVal.put(COOKIES_NAME_COL, cookie.getName());
        cookieVal.put(COOKIES_VALUE_COL, cookie.getValue());
 
        mDatabase.insert("cookies", null, cookieVal);
      
    }
}

posted @ 2018-03-28 16:02  小毛驴  阅读(104)  评论(0编辑  收藏  举报