android讀寫cookie的方法示例
做了一個(gè)android網(wǎng)絡(luò)應(yīng)用,要求用自己實(shí)現(xiàn)的webview去訪問web網(wǎng)站,并且在遠(yuǎn)程登錄成功之后把cookie寫入到手機(jī),保留用作以后的自動(dòng)登錄。找了好多資料。發(fā)覺讀取cookies倒還用的很普遍,可是通過程序?qū)慶ookie卻沒有太多資料。
先來看一下如何讀取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是采用一個(gè)txt文本文件的形式來存儲(chǔ)cookie。而android是將cookie存儲(chǔ)在數(shù)據(jù)庫中。具體的介紹在《android cookie存儲(chǔ)位置》一文中有介紹。我們都知道,android每個(gè)應(yīng)用程序的存儲(chǔ)空間都是獨(dú)立的。不管使用preference還是database存儲(chǔ),都會(huì)在每個(gè)/data/data/package name/下面進(jìn)行存儲(chǔ)(preference存儲(chǔ)在/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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)手冊(cè)自定義Switch開關(guān)按鈕控件
這篇文章主要為大家介紹了Android開發(fā)手冊(cè)自定義Switch開關(guān)按鈕控件的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android中三種onClick的實(shí)現(xiàn)方式與對(duì)比
這篇文章主要為大家詳細(xì)介紹了Android中三種onClick的實(shí)現(xiàn)方式以及詳細(xì)對(duì)比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Android開發(fā)筆記 Handler使用總結(jié)
當(dāng)應(yīng)用程序啟動(dòng)時(shí),Android首先會(huì)開啟一個(gè)主線程(也就是UI線程),主線程為管理界面中的UI控件,進(jìn)行事件分發(fā)2012-11-11
Android?studio實(shí)現(xiàn)日期?、時(shí)間選擇器與進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)日期、時(shí)間選擇器與進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android開發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)之Notification手機(jī)狀態(tài)欄通知用法,結(jié)合實(shí)例形式分析了Android Notification手機(jī)狀態(tài)欄通知的常見函數(shù)、功能及使用技巧,需要的朋友可以參考下2019-03-03
Android獲取WebView加載url的請(qǐng)求錯(cuò)誤碼 【推薦】
這篇文章主要介紹了Android獲取WebView加載url的請(qǐng)求錯(cuò)誤碼 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06

