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

来源:互联网 发布:c语言解惑 pdf 编辑:程序博客网 时间:2024/05/02 09:48

最近被这个项目折腾死了

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

实现思路:

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

view plaincopy to clipboardprint?
  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信息

view plaincopy to clipboardprint?
  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信息

view plaincopy to clipboardprint?
  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.         }  
原创粉丝点击