android开发中webview保存cookie问题的解决

最近被这个项目折腾死了

客户端登录---客户端九宫格---webview内容功能模块

实现思路:

登录还是调用原来的servlet进行验证

  1. List<NameValuePair> myList = new ArrayList<NameValuePair>();  
  2.                  myList.add(new BasicNameValuePair("STAFFID", username));  
  3.                  myList.add(new BasicNameValuePair("PWD", password));  
  4.                  HttpParams params = new BasicHttpParams();  
  5.                  DefaultHttpClient client = new DefaultHttpClient(params);  
  6.                  HttpPost post = new HttpPost(actionURL);  
  7.                  HttpResponse response = null;  
  8.                  BasicResponseHandler myHandler = new BasicResponseHandler();  
  9.                  String endResult = null;  
  10.                  try { post.setEntity(new UrlEncodedFormEntity(myList)); }  
  11.                  catch (UnsupportedEncodingException e)  
  12.                  { e.printStackTrace(); }  
  13.                  try { response = client.execute(post); }  
  14.                  catch (ClientProtocolException e)  
  15.                  { e.printStackTrace(); }  
  16.                  catch (IOException e)  
  17.                  { e.printStackTrace(); }  
 

验证成功后保存cookie并保存到SharedPreferences

//获取cookie信息

  1. List<Cookie> cookies = client.getCookieStore().getCookies();  
  2.          if (cookies.isEmpty()) {  
  3.           Log.i(TAG, "-------Cookie NONE---------");  
  4.        } else {                 
  5.          for (int i = 0; i < cookies.size(); i ) {  
  6.           //保存cookie  
  7.           cookie = cookies.get(i);  
  8.          Log.d(TAG, cookies.get(i).getName() "=" cookies.get(i).getValue() );  
  9.          if(cookies.get(i).getName().equals("loginStaffId"))  {               
  10.           //保存登录信息,下次无需登录  
  11.           String PREFS_NAME = "nma.qztc.com";  
  12.           SharedPreferences settings = v.getContext().getSharedPreferences(PREFS_NAME, 0);  
  13.           SharedPreferences.Editor editor = settings.edit();               
  14.           editor.putString("staff_id", username);  
  15.           editor.putString("pwd", password);  
  16.           editor.commit();  
  17.           return true;  
  18.          }  

 

-----------------------------------------------

将cookie信息带入到webview中,之前总是出现有时cookie读取成功有时不成功,找了半天发现将

cookieManager.removeSessionCookie();
这句去掉就好了,暂时还没有出现什么问题,由于原来的web应用是采用session验证,所以在读取cookie成功后也将session信息写入,这样就双保险了

//设置cookie信息

  1. Cookie sessionCookie = OnLoginListenerImpl.cookie;  
  2.         CookieSyncManager.createInstance(this);  
  3.         CookieManager cookieManager = CookieManager.getInstance();  
  4.         if (sessionCookie != null) {  
  5.         //cookieManager.removeSessionCookie();  
  6.         cookieString = sessionCookie.getName() "=" sessionCookie.getValue() "; domain=" sessionCookie.getDomain();  
  7.         Log.d("----nma cookie-----",cookieString);  
  8.         //Log.d("----url-----",getString(getResources().getIdentifier(url,"string", getPackageName())));  
  9.         cookieManager.setCookie(getString(getResources().getIdentifier(url,"string", getPackageName())), cookieString);  
  10.         CookieSyncManager.getInstance().sync();  
  11.         }  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值