Read and write operations on cookies under Android (with Demo)

Reprinted from: http://www.67tgb.com/?p=536

 Cookie is the data stored on the user's local terminal in order to identify the user's identity and track the session. It is also often used in Android. Next, we will introduce how cookies are read and written in Android.

   Cookie is actually a string, generated by the server. When requesting some URLs that require cookies, set the string to the Header.

   Finding a scenario that reads and writes cookies is fairly easy:

The scene of writing cookies

   such as logging in. When the user successfully logs in for the first time, we will obtain the cookie from the server and store it in the local file of the user's mobile phone. When the user opens the app next time, the local cookie will be read first. If the cookie has not expired, the user will be directly guided to the corresponding interface and no longer log in.

Scenario of reading cookies

   In addition to login, there are some special server requests that need to send local cookie information to the server together.

   Go directly to the code to get cookies:

 

[java]  view plain copy  
 
  View code snippets on CODE Derive to my code slice
  1. /** 
  2.      * Get standard cookies and store them 
  3.      * @param httpClient 
  4.      */  
  5.      privatevoid getCookie(DefaultHttpClient httpClient) {   
  6.           List<Cookie> cookies = httpClient.getCookieStore().getCookies();  
  7.           StringBuffer sb = new StringBuffer();  
  8.           for (int i = 0; i < cookies.size(); i++) {  
  9.               Cookie cookie = cookies.get(i);  
  10.               String cookieName = cookie.getName();  
  11.               String cookieValue = cookie.getValue();  
  12.               if (!TextUtils.isEmpty(cookieName)  
  13.                        && !TextUtils.isEmpty(cookieValue)) {  
  14.                   sb.append(cookieName + "=" );  
  15.                   sb.append(cookieValue + ";" );  
  16.              }  
  17.          }  
  18.          Log. e( "cookie", sb.toString());  
  19.          Util. savePreference( "cookie", sb.toString());  
  20.     }  


获得了Cookie之后,一般存储到本地文件或者数据库中。在访问需要携带Cookie信息的url时,将Cookie读出,并设置到Header中,例如:

 

 

[java]  view plain  copy
 
  View code snippets on CODE Derive to my code slice
  1. DefaultHttpClient httpClient = new DefaultHttpClient();  
  2. HttpGet httpGet = new HttpGet(URLContainer.getPlayHistoryInCloud());  
  3. httpGet.setHeader( "Cookie" , Util.getPreference( "cookie"));  
  4. HttpResponse httpResponse;  
  5.    try {  
  6.         httpResponse = httpClient.execute(httpGet);  
  7.         InputStream is = httpResponse.getEntity().getContent();  
  8.         String jsonString = Util.convertStreamToString(is);  
  9.          return jsonString;  
  10.        } catch (ClientProtocolException e) {                         
  11.                e.printStackTrace ();  
  12.        } catch (IOException e) {                       
  13.                 e.printStackTrace ();  
  14.        }  

 

 

As shown below:

  Attached Demo:  Click me to download

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326726485&siteId=291194637