如何在android中對cookie進(jìn)行讀寫?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)制作后付款的網(wǎng)站建設(shè)流程,更有吳起免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
讀取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的源碼,發(fā)現(xiàn)android默認(rèn)的browser增加cookie是在數(shù)據(jù)庫中增加記錄,和window不同,win是采用一個txt文本文件的形式來存儲cookie。而android是將cookie存儲在數(shù)據(jù)庫中。具體的介紹在《android cookie存儲位置》一文中有介紹。我們都知道,android每個應(yīng)用程序的存儲空間都是獨(dú)立的。不管使用preference還是database存儲,都會在每個/data/data/package name/下面進(jìn)行存儲(preference存儲在/data/data/package name/shared_prefs/xxxx.xml)。前面也說到cookie是存在數(shù)據(jù)庫中,那么如果采用非瀏覽器訪問網(wǎng)絡(luò)需要保留cookie的話我們就應(yīng)該在database中建立cookies表,并且存入相應(yīng)的cookies數(shù)據(jù)。仿照默認(rèn)broswer的代碼:
/**聲明一些數(shù)據(jù)庫操作的常量*/ 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); //創(chuàng)建cookie數(shù)據(jù)庫 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); } }
關(guān)于如何在android中對cookie進(jìn)行讀寫問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
網(wǎng)站欄目:如何在android中對cookie進(jìn)行讀寫
網(wǎng)頁鏈接:http://chinadenli.net/article20/ppcoco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站制作、微信公眾號、、靜態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)