android开发中webview保存cookie问题的解决
2011-04-16 15:02
871 查看
最近被这个项目折腾死了
客户端登录---客户端九宫格---webview内容功能模块
实现思路:
登录还是调用原来的servlet进行验证
view plaincopy to clipboardprint?
List<NameValuePair> myList = new ArrayList<NameValuePair>();
myList.add(new BasicNameValuePair("STAFFID", username));
myList.add(new BasicNameValuePair("PWD", password));
HttpParams params = new BasicHttpParams();
DefaultHttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost(actionURL);
HttpResponse response = null;
BasicResponseHandler myHandler = new BasicResponseHandler();
String endResult = null;
try { post.setEntity(new UrlEncodedFormEntity(myList)); }
catch (UnsupportedEncodingException e)
{ e.printStackTrace(); }
try { response = client.execute(post); }
catch (ClientProtocolException e)
{ e.printStackTrace(); }
catch (IOException e)
{ e.printStackTrace(); }
验证成功后保存cookie并保存到SharedPreferences
//获取cookie信息
view plaincopy to clipboardprint?
List<Cookie> cookies = client.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.i(TAG, "-------Cookie NONE---------");
} else {
for (int i = 0; i < cookies.size(); i ) {
//保存cookie
cookie = cookies.get(i);
Log.d(TAG, cookies.get(i).getName() "=" cookies.get(i).getValue() );
if(cookies.get(i).getName().equals("loginStaffId")) {
//保存登录信息,下次无需登录
String PREFS_NAME = "nma.qztc.com";
SharedPreferences settings = v.getContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("staff_id", username);
editor.putString("pwd", password);
editor.commit();
return true;
}
-----------------------------------------------
将cookie信息带入到webview中,之前总是出现有时cookie读取成功有时不成功,找了半天发现将
cookieManager.removeSessionCookie();
这句去掉就好了,暂时还没有出现什么问题,由于原来的web应用是采用session验证,所以在读取cookie成功后也将session信息写入,这样就双保险了
//设置cookie信息
view plaincopy to clipboardprint?
Cookie sessionCookie = OnLoginListenerImpl.cookie;
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
//cookieManager.removeSessionCookie();
cookieString = sessionCookie.getName() "=" sessionCookie.getValue() "; domain=" sessionCookie.getDomain();
Log.d("----nma cookie-----",cookieString);
//Log.d("----url-----",getString(getResources().getIdentifier(url,"string", getPackageName())));
cookieManager.setCookie(getString(getResources().getIdentifier(url,"string", getPackageName())), cookieString);
CookieSyncManager.getInstance().sync();
}
客户端登录---客户端九宫格---webview内容功能模块
实现思路:
登录还是调用原来的servlet进行验证
view plaincopy to clipboardprint?
List<NameValuePair> myList = new ArrayList<NameValuePair>();
myList.add(new BasicNameValuePair("STAFFID", username));
myList.add(new BasicNameValuePair("PWD", password));
HttpParams params = new BasicHttpParams();
DefaultHttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost(actionURL);
HttpResponse response = null;
BasicResponseHandler myHandler = new BasicResponseHandler();
String endResult = null;
try { post.setEntity(new UrlEncodedFormEntity(myList)); }
catch (UnsupportedEncodingException e)
{ e.printStackTrace(); }
try { response = client.execute(post); }
catch (ClientProtocolException e)
{ e.printStackTrace(); }
catch (IOException e)
{ e.printStackTrace(); }
验证成功后保存cookie并保存到SharedPreferences
//获取cookie信息
view plaincopy to clipboardprint?
List<Cookie> cookies = client.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.i(TAG, "-------Cookie NONE---------");
} else {
for (int i = 0; i < cookies.size(); i ) {
//保存cookie
cookie = cookies.get(i);
Log.d(TAG, cookies.get(i).getName() "=" cookies.get(i).getValue() );
if(cookies.get(i).getName().equals("loginStaffId")) {
//保存登录信息,下次无需登录
String PREFS_NAME = "nma.qztc.com";
SharedPreferences settings = v.getContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("staff_id", username);
editor.putString("pwd", password);
editor.commit();
return true;
}
-----------------------------------------------
将cookie信息带入到webview中,之前总是出现有时cookie读取成功有时不成功,找了半天发现将
cookieManager.removeSessionCookie();
这句去掉就好了,暂时还没有出现什么问题,由于原来的web应用是采用session验证,所以在读取cookie成功后也将session信息写入,这样就双保险了
//设置cookie信息
view plaincopy to clipboardprint?
Cookie sessionCookie = OnLoginListenerImpl.cookie;
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
//cookieManager.removeSessionCookie();
cookieString = sessionCookie.getName() "=" sessionCookie.getValue() "; domain=" sessionCookie.getDomain();
Log.d("----nma cookie-----",cookieString);
//Log.d("----url-----",getString(getResources().getIdentifier(url,"string", getPackageName())));
cookieManager.setCookie(getString(getResources().getIdentifier(url,"string", getPackageName())), cookieString);
CookieSyncManager.getInstance().sync();
}
相关文章推荐
- android开发中webview保存cookie问题的解决
- android开发中webview保存cookie问题的解决
- android开发中webview保存cookie问题的解决
- android h5 js 混合开发解决webview加载白屏,不加载网页的问题(硬件加速网页渲染,优化体验)
- android webview cookie 同步时有时候会出现同步不成功的问题解决
- Android开发——webview获取H5的Cookie并保存在本地
- Android WebView开发中无法调音量的问题解决
- Android WebView的cookie第一次加载不工作的问题的解决
- android 通过设置cookie解决app 登录后WebView还要重新登陆问题
- [Android开发] WebView遇到的问题以及解决
- android——webview开发:跨域请求问题的解决方式之一
- Android开发——webview获取H5的Cookie并保存在本地
- Android开发之如何在WebView中点击链接使用自身打开(解决出现的选择使用第三方应用的问题)
- android webview H5开发若干问题之问题二:cookie问题
- Android WebView开发问题及优化汇总
- android webview 保存cookie
- Android WebView开发问题及优化汇总
- Android WebView开发问题及优化汇总
- android webview ZoomButtonsController 导致android.view.WindowLeaked 问题解决
- Android Cookie实现webview的同步登陆(主要是避免webview的再次登录)解决