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里面增加变量 //也可以在普通类中使用静态变量,这里使用的是静态变量方式。 public class NetworkTool { public static CookieStore 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