You can use sharedpreferences or SQLite to save user information.
Private Static hashmap <string, string> cookiecontiner = new hashmap <string, string> ();
/**
* Save cookie
* @ Param Resp
*/
Public void savecookies (httpresponse)
{
Header [] headers = httpresponse. getheaders ("Set-cookie ");
String headerstr = headers. tostring ();
If (headers = NULL)
Return;
For (INT I = 0; I {
String cookie = headers [I]. getvalue ();
String [] cookievalues = cookie. Split (";");
For (Int J = 0; j <cookievalues. length; j ++)
{
String [] keypair = cookievalues [J]. Split ("= ");
String key = keypair [0]. Trim ();
String value = keypair. length> 1? Keypair [1]. Trim ():"";
Cookiecontiner. Put (Key, value );
}
}
}
/**
* Add cookie
* @ Param request
*/
Public void addcookies (httppost request)
{
Stringbuilder sb = new stringbuilder ();
Iterator iter = cookiecontiner. entryset (). iterator ();
While (ITER. hasnext ()){
Map. Entry entry = (Map. Entry) ITER. Next ();
String key = entry. getkey (). tostring ();
String val = entry. getvalue (). tostring ();
SB. append (key );
SB. append ("= ");
SB. append (VAL );
SB. append (";");
}
Request. addheader ("cookie", SB. tostring ());
}
An android network application requires you to use your own webview to access the web site. After successful remote logon, the cookie is written to your mobile phone and reserved for automatic logon. I found a lot of information. It is found that reading cookies is still common,ProgramWriting cookies does not have much information.
Let's take a look at how to read cookies:
Try
{
Defaulthttpclient httpclient = new defaulthttpclient ();
Httpget = new httpget ("http://www.hlovey.com /");
Httpresponse response = httpclient.exe cute (httpget );
Httpentity entity = response. getentity ();
List & lt; cookie & gt; cookies = httpclient. getcookiestore (). getcookies ();
If (entity! = NULL ){
Entity. consumecontent ();
}
If (cookies. isempty ()){
Log. I (TAG, "NONE ");
} Else {
For (INT I = 0; I & lt; 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
}
By analyzing COM. android. browser source code. It is found that the default browser for Android adds cookies to the database. Unlike windows, win uses a TXT text file to store cookies. Android stores cookies in databases. The specific introduction is described in Android cookie storage location. We all know that the storage space of each android application is independent. Whether preference or database storage is used, preference is stored under/data/package name/(preference is stored in/data/package name/shared_prefs/xxxx. XML ). As mentioned above, cookies exist in databases. If you need to retain cookies when using a non-browser to access the network, we should create a cookie table in the database and store the corresponding cookies. Follow the default broswerCode :
/** Declare constants of some Database Operations */
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 );
// Create a cookie Database
If (mdatabase! = NULL ){
// Cookies
Mdatabase.exe csql ("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.exe csql ("create index if not exists cookiesindex on"
+ "Cookies" + "(PATH )");
}
}
/* Write cookie */
Public void addcookie (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 );
}
}