Andrew Xutils's session was acquired and Cookiestore used

Source: Internet
Author: User

Before using the Xutils framework did the project, at that time because the session tangled long, but solved, now in retrospect, as if no backup, hurriedly write a note backup, later convenient to use.

The main is to log on to the server, you should save a login status, after landing, will save a login status, only save this shape to have access to the corresponding content, otherwise, returned is a bunch of HTML-formatted pages.

At this point, you will consider saving the cookie. Directly on the code bar:

Here is the login interface:

Mainactivity.java the corresponding import

Import Org.apache.http.client.CookieStore; Import Org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient;
Mainactivity.java
1 Private voidHttplogin () {2         FinalHttputils http =Newhttputils ();3Requestparams params =Newrequestparams ();4String url = "http://192.168.1.60/auto/login_Login.action?oper.operatorName=123&oper.password=123";5         FinalString urlpost= "Http://192.168.1.60/auto/login_Login.action";6Params.addbodyparameter ("Oper.operatorname", "123");7Params.addbodyparameter ("Oper.password", "123");8 http.send (HttpRequest.HttpMethod.POST,9 Urlpost,Ten params, One                 NewRequestcallback<string>() { A  - @Override -                      Public voidOnStart () { the pg.show (); -LOG.D ("Jack", urlpost); -                     } -  + @Override -                      Public voidOnloading (LongTotalLongCurrentBooleanisuploading) { + pg.show (); A                     } at  - @Override -                      Public voidOnsuccess (responseinfo<string>responseinfo) { - Pg.dismiss (); -LOG.D ("Jack", Responseinfo.result); -                         //=========================== in                         //get the SessionID ....... ........... -  to                        defaulthttpclient dh = (defaulthttpclient) http.gethttpclient (); +                         Mycookiestore.cookiestore = Dh.getcookiestore (); -Cookiestore cs =Dh.getcookiestore (); thelist<cookie> cookies =cs.getcookies (); *String AA =NULL; $                          for(inti = 0; I < cookies.size (); i++) {Panax Notoginseng                             if("Jsessionid". Equals (Cookies.get (i). GetName ())) { -AA =Cookies.get (i). GetValue (); the                                  Break; +                             } A                         } theLOG.D ("Jack", "compare SessionID" +aa); +                         //=========================== -Intent Intent =NewIntent (mainactivity. This, Secondactivity.class); $ startactivity (intent); $                     } -  - @Override the                      Public voidonfailure (httpexception error, String msg) { - Pg.dismiss ();WuyiLOG.D ("Jack", "haha" +msg); the                     } -             }); Wu}

Key code:

1 //===========================2                         //get the SessionID ....... ...........3Defaulthttpclient DH =(defaulthttpclient) http.gethttpclient ();4Mycookiestore.cookiestore =Dh.getcookiestore ();5Cookiestore cs =Dh.getcookiestore ();6list<cookie> cookies =cs.getcookies ();7String AA =NULL;8                          for(inti = 0; I < cookies.size (); i++) {9                             if("Jsessionid". Equals (Cookies.get (i). GetName ())) {TenAA =Cookies.get (i). GetValue (); One                                  Break; A                             } -                         } -LOG.D ("Jack", "compare SessionID" +aa); the                         //===========================

The Mycookiestore in this is a custom save class:

Mycookiestore.java
1 Import Org.apache.http.client.CookieStore; 2 3  Public class Mycookiestore {45      Public Static Cookiestore cookiestore=null; 6     7 }

Finally, when accessing the server after landing, add a word:

Http.configcookiestore (Mycookiestore.cookiestore);

Let's take a look at the code in a class:

Secondactivity.java
1 Private voidHttplogin () {2Httputils http =Newhttputils ();3         FinalString urlget= "Http://192.168.1.60/auto/carModel_queryPL.action";4 Http.configcookiestore (mycookiestore.cookiestore);5 http.send (HttpRequest.HttpMethod.POST,6 Urlget,7                 NewRequestcallback<string>() {8 9 @OverrideTen                      Public voidOnStart () { One pg.show (); ALOG.D ("Jack", urlget); -                     } -  the @Override -                      Public voidOnloading (LongTotalLongCurrentBooleanisuploading) { - pg.show (); -                     } +  - @Override +                      Public voidOnsuccess (responseinfo<string>responseinfo) { A Pg.dismiss (); atLOG.D ("Jack", "displacement" +responseinfo.result); -                     } -  - @Override -                      Public voidonfailure (httpexception error, String msg) { - Pg.dismiss (); inLOG.D ("Jack", "displacement" +msg); -                     } to             }); +}

Andrew Xutils's session was acquired and Cookiestore used

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.