首页 > 代码库 > Andrew XUtils的session获得和cookieStore使用

Andrew XUtils的session获得和cookieStore使用

之前使用过XUtils框架做过项目,当时因为session纠结了好久,但是解决了,现在回想起来,好像没有备份,赶紧写份笔记备份下,以后方便使用。

主要是登陆服务器后,应该保存一个登陆状态,登陆后,会保存个登陆状态,只有保存这个状才能有权限访问对应的内容,否则,返回的是一堆html格式的网页。

这时候,就会考虑到保存Cookie。直接上代码吧:

这里是登陆界面:

MainActivity.java对应的import

import org.apache.http.client.CookieStore;import org.apache.http.cookie.Cookie;import org.apache.http.impl.client.DefaultHttpClient;
MainActivity.java
 1 private void httpLogin() { 2         final HttpUtils http = new HttpUtils(); 3         RequestParams params = new RequestParams(); 4         String url = "http://192.168.1.60/auto/login_Login.action?oper.operatorName=123&oper.password=123"; 5         final String urlPost="http://192.168.1.60/auto/login_Login.action"; 6         params.addBodyParameter("oper.operatorName","123"); 7         params.addBodyParameter("oper.password","123"); 8         http.send(HttpRequest.HttpMethod.POST, 9                 urlPost,10                 params,11                 new RequestCallBack<String>() {12 13                     @Override14                     public void onStart() {15                         pg.show();16                         Log.d("jack",urlPost);17                     }18 19                     @Override20                     public void onl oading(long total, long current, boolean isUploading) {21                         pg.show();22                     }23 24                     @Override25                     public void onSuccess(ResponseInfo<String> responseInfo) {26                         pg.dismiss();27                         Log.d("jack",responseInfo.result);28                         //===========================29                         // 取得sessionid.........................30 31                         DefaultHttpClient dh = (DefaultHttpClient) http.getHttpClient();32                         MyCookieStore.cookieStore = dh.getCookieStore();33                         CookieStore cs = dh.getCookieStore();34                         List<Cookie> cookies = cs.getCookies();35                         String aa = null;36                         for (int i = 0; i < cookies.size(); i++) {37                             if ("JSESSIONID".equals(cookies.get(i).getName())) {38                                 aa = cookies.get(i).getValue();39                                 break;40                             }41                         }42                         Log.d("jack", "比较sessionid" + aa);43                         //===========================44                         Intent intent = new Intent(MainActivity.this,SecondActivity.class);45                         startActivity(intent);46                     }47 48                     @Override49                     public void onFailure(HttpException error, String msg) {50                         pg.dismiss();51                         Log.d("jack","哈哈"+msg);52                     }53             });54     }

关键代码:

 1 //=========================== 2                         // 取得sessionid......................... 3                         DefaultHttpClient dh = (DefaultHttpClient) http.getHttpClient(); 4                         MyCookieStore.cookieStore = dh.getCookieStore(); 5                         CookieStore cs = dh.getCookieStore(); 6                         List<Cookie> cookies = cs.getCookies(); 7                         String aa = null; 8                         for (int i = 0; i < cookies.size(); i++) { 9                             if ("JSESSIONID".equals(cookies.get(i).getName())) {10                                 aa = cookies.get(i).getValue();11                                 break;12                             }13                         }14                         Log.d("jack", "比较sessionid" + aa);15                         //===========================

这里面的MyCookieStore是自定义的一个保存类:

MyCookieStore.java
1 import org.apache.http.client.CookieStore;2 3 public class MyCookieStore {4 5     public static CookieStore cookieStore=null;6     7 }

最后就是在登陆后访问服务器的时候添加一句话:

http.configCookieStore(MyCookieStore.cookieStore);

给个类里的代码看看吧:

SecondActivity.java
 1 private void httpLogin() { 2         HttpUtils http = new HttpUtils(); 3         final String urlGet="http://192.168.1.60/auto/carModel_queryPL.action"; 4         http.configCookieStore(MyCookieStore.cookieStore); 5         http.send(HttpRequest.HttpMethod.POST, 6                 urlGet, 7                 new RequestCallBack<String>() { 8  9                     @Override10                     public void onStart() {11                         pg.show();12                         Log.d("jack",urlGet);13                     }14 15                     @Override16                     public void onl oading(long total, long current, boolean isUploading) {17                         pg.show();18                     }19 20                     @Override21                     public void onSuccess(ResponseInfo<String> responseInfo) {22                         pg.dismiss();23                         Log.d("jack","排量"+responseInfo.result);24                     }25 26                     @Override27                     public void onFailure(HttpException error, String msg) {28                         pg.dismiss();29                         Log.d("jack","排量"+msg);30                     }31             });32     }

 

Andrew XUtils的session获得和cookieStore使用