For the login function itself is not any special, use HttpClient to the server post username password.
However, in order to maintain the status of the login (when switching between activity to let the site know that the user is always logged in state), you need to read and write cookies.
HttpClient is quite powerful, and reading and writing cookies is very easy:
Cookiestore cookies= (abstracthttpclient) client). Getcookiestore ()//Read Cookie
((abstracthttpclient) client). Setcookiestore (cookies);//write Cookies
Another problem is that in order to use a common cookie between the various activity, a global variable is needed to solve the problem. For Java, static classes can be used, but for Android, a more compliant approach to Android is to use the application class that owns these activity:
First declare a application class to access cookies:
public class MyApp extends application {
private cookiestore cookies;
Public Cookiestore GetCookie () {return
cookies;
}
public void Setcookie (Cookiestore cks) {
cookies = cks;
}
}
In addition, to bind the class declared above in the Androidmanifest.xml file to the application that contains all the activity, the method is to add Android:name= ". MyApp" to the application label.
This allows the "global variable" to be used in the activity:
Set the cookie
myApp Appcookie = ((myApp) getapplication ());
Appcookie.setcookie (cookies);
Read Cookie
myApp Appcookie = ((myApp) Getapplicationcontext ());
cookies = Appcookie.getcookie ();
After the above steps, the Android program completes a Web login to keep the login state.
Ps.
The method of obtaining statuscode from a httpclient:
HttpResponse response = Client.execute (mypost);
int statuscode = Response.getstatusline (). Getstatuscode ();
To do is to log into their own library account, used to view the books they borrowed,
Looked at the library code, found that the post user name and password,
And in the code implementation has two main difficulties: first, save account password two, save cookies
Then, the first can be used before mentioned sharedpreference, each time directly from the sharedpreference to get the account name and password on it, do not need to enter every time
The second how to get the server cookie, know this session ID
Through the Getcookiestore in HttpClient
list<cookie> cookies = Httpclient.getcookiestore (). GetCookies ();
if (Cookies.isempty ()) {
log.i (TAG, "-------Cookie NONE---------");
} else {for
(int i = 0; i < cookies.si Ze (); i) {
//save cookie
cookie = cookies.get (i);
LOG.D (TAG, Cookies.get (i). GetName () "=" Cookies.get (i) getValue ());
After you get the session ID, how do you add it to our post or getting request?
HttpPost HttpPost = new HttpPost (access address);
Httppost.setheader ("Cookie", "jsessionid=" + the SessionID we store in static variables);
HttpResponse HttpResponse = Httpclient.execute (httppost);
HttpGet request = new HttpGet (url+ "?") +params);
The above is on the Android login Web as well as the login to maintain, the cookie management, the need for friends can refer to.