国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android讀寫cookie的方法示例

2019-12-12 00:52:19
字體:
來源:轉載
供稿:網友

做了一個android網絡應用,要求用自己實現的webview去訪問web網站,并且在遠程登錄成功之后把cookie寫入到手機,保留用作以后的自動登錄。找了好多資料。發覺讀取cookies倒還用的很普遍,可是通過程序寫cookie卻沒有太多資料。

先來看一下如何讀取cookie吧:

try    {     DefaultHttpClient httpclient = new DefaultHttpClient();     HttpGet httpget = new HttpGet("http://www.hlovey.com");     HttpResponse response = httpclient.execute(httpget);     HttpEntity entity = response.getEntity();     List<Cookie> cookies = httpclient.getCookieStore().getCookies();     if (entity != null) {       entity.consumeContent();     }         if (cookies.isEmpty()) {      Log.i(TAG, "NONE");     } else {       for (int i = 0; i < cookies.size(); i++) {               Log.i(TAG,"- domain " + cookies.get(i).getDomain());        Log.i(TAG,"- path " + cookies.get(i).getPath());        Log.i(TAG,"- value " + cookies.get(i).getValue());        Log.i(TAG,"- name " + cookies.get(i).getName());        Log.i(TAG,"- port " + cookies.get(i).getPorts());        Log.i(TAG,"- comment " + cookies.get(i).getComment());        Log.i(TAG,"- commenturl" + cookies.get(i).getCommentURL());        Log.i(TAG,"- all " + cookies.get(i).toString());       }     }     httpclient.getConnectionManager().shutdown();        }catch(Exception e){     //Todo    }finally{    //Todo         }

通過分析com.android.browser的源碼,發現android默認的browser增加cookie是在數據庫中增加記錄,和window不同,win是采用一個txt文本文件的形式來存儲cookie。而android是將cookie存儲在數據庫中。具體的介紹在《android cookie存儲位置》一文中有介紹。我們都知道,android每個應用程序的存儲空間都是獨立的。不管使用preference還是database存儲,都會在每個/data/data/package name/下面進行存儲(preference存儲在/data/data/package name/shared_prefs/xxxx.xml)。前面也說到cookie是存在數據庫中,那么如果采用非瀏覽器訪問網絡需要保留cookie的話我們就應該在database中建立cookies表,并且存入相應的cookies數據。仿照默認broswer的代碼:

/**聲明一些數據庫操作的常量*/ 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);     }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南宁市| 甘德县| 洪湖市| 玉山县| 信宜市| 本溪| 襄垣县| 江源县| 武威市| 高密市| 镇康县| 库伦旗| 连江县| 平顶山市| 江达县| 安泽县| 长春市| 仪征市| 土默特左旗| 揭西县| 淮滨县| 扎兰屯市| 筠连县| 芦溪县| 大埔县| 涿州市| 鄄城县| 蒲城县| 清水河县| 乐昌市| 文成县| 涟源市| 扎鲁特旗| 太原市| 隆林| 桃园县| 长沙市| 徐水县| 寿宁县| 岳普湖县| 定陶县|