如何在android中对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);
}
}
关于如何在android中对cookie进行读写问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。