Android Local save cookie and restore to Cookiestore

Source: Internet
Author: User

Save cookies, read cookies, and determine if a cookie exists

"' Java

public class Local {

private static sharedpreferences gkfx;

public static void setshared (Sharedpreferences shared) {local.shared = shared;}

public static void Savestrcookie (String cookie) {Sharedpreferences.editor Editor = Shared.edit (); Editor.putstring ( Constant.cookie_name, COOKIE); Editor.commit ();}

public static Boolean Hascookie () {String sessionID = shared.getstring (constant.cookie_name, NULL);

if (sessionid = = NULL | | sessionId.equals ("")) {logutil.i ("Hascookie return false"); return false;} Logutil.i ("Hascookie return True"); return true;}

public static string GetCookie () {string sessionid = shared.getstring (constant.cookieNAME, NULL); LOGUTIL.E ("====>getcookie =" + _sessionID); return sessionID;}

public static void Savecookie (Defaulthttpclient httpClient) {List cookie = Httpclient.getcookiestore (). GetCookie S (); String Strcookie = ""; for (Cookie cookie:cookies) {Strcookie = Cookie.getvalue (). toString (); LOGUTIL.E ("====>savecookie =" + Strcookie); Savestrcookie (Strcookie);}} ```

Add cookies to Cookiestore. The main thing is Basicclientcookie this class

"' Java

private void Setrequestcookies (Defaulthttpclient client) {

String cookie = Local.getcookie ();

LOG.I ("Cookie VALUE", cookie);

if (! Textutils.isempty (cookie)) {

Basicclientcookie C = new Basicclientcookie (Constant.cookie_name,

Cookies);

C.setversion (0);

C.setpath ("/");

C.setdomain (Constant.cookie_domain);

Client.getcookiestore (). Addcookie (c);

}

}```

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.