Android uses Xutils to save cookies for access after login

Source: Internet
Author: User

Http://blog.memecodes.com/340.html

In the HTTP connection, there will be a cookie status, need to access the login page, you must save the cookie for authentication, and each time you send a request, the saved cookie will be brought together to achieve login access. Development, the general use of components, frameworks, platforms and other methods for rapid development, this article will describe how to use the Xutils framework to save cookies.

Save cookies with Xutils
1 HttpUtils com.lidroid.xutils.HttpUtils.configCookieStore(CookieStore cookieStore)

In Xutls, there is a configcookiestore used to set the cookie object in the request. The parameter type Org.apache.http.client.CookieStore, that is, after logging in, save the Cookiestore type cookie.

12345 //实现全局共享,有很多种方法,可以使用自定义Application里面增加变量//也可以在普通类中使用静态变量,这里使用的是静态变量方式。publicclass NetworkTool {    public staticCookieStore cookieStore=null;}

Save cookies to static variables after successful login

123 //http为HttpUtils实例DefaultHttpClient httpClient = (DefaultHttpClient) http.getHttpClient();NetworkTool.cookieStore = httpClient.getCookieStore();

Finally, you only need to set a saved cookie in the request.

12 //http为HttpUtils实例http.configCookieStore(NetworkTool.cookieStore);
Save cookies in non-xutils

Cookies exist in HttpClient, and a simple understanding is to use uniform httpclient in all requests, or to get cookiestore, consistent with xutils.

Persist cookies

Sometimes it is necessary to persist the cookie for the application to exit and still be available;

12 //可以使用SharedPreferences、SQLite、XML等方法保存List<Cookie> cookies = httpclient.getCookieStore().getCookies();

Android uses Xutils to save cookies for access after login

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.