Handling cookies when Android logs on to the Web _android

Source: Internet
Author: User
Tags session id setcookie

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.