Android菜鸟学习笔记(WebView加载html页面,页面提交数据问题)
2014-09-15 18:05
966 查看
在开发过程中,有一个问卷调查功能,问卷是url用webView加载html页面出来,在html页面提交时发现session没和webview的同步导致提交失败,这个问题困扰了我很久,后来在前辈的共同研究下,终于找到办法:获取html页面提交的url,然后对url做一次cookie同步操作,然后再提交就可以了。
//cookie同步方法:
public static void synCookies(Context context, String url){
DefaultHttpClient httpclient2 = (DefaultHttpClient)RequestUtils.getHttpClient().getHttpClient();
//CookieStore cookieStores =(CookieStore)CloudUtils.getHttpClient().getHttpContext().getAttribute(ClientContext.COOKIE_STORE);
List<Cookie> cookies = httpclient2.getCookieStore().getCookies();
if (!cookies.isEmpty()){
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
//sync all the cookies in the httpclient with the webview by generating cookie string
for (Cookie cookie : cookies){
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
cookieManager.setCookie(url, cookieString);
CookieSyncManager.getInstance().sync();
}
}
}
//form表单获取url设置同步cookie
webView.setWebViewClient(new WebViewClient() {
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
{
// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
AppConfig.synCookies(QuestionnaireDetailActivity.this,url);
return null;
}
});
//cookie同步方法:
public static void synCookies(Context context, String url){
DefaultHttpClient httpclient2 = (DefaultHttpClient)RequestUtils.getHttpClient().getHttpClient();
//CookieStore cookieStores =(CookieStore)CloudUtils.getHttpClient().getHttpContext().getAttribute(ClientContext.COOKIE_STORE);
List<Cookie> cookies = httpclient2.getCookieStore().getCookies();
if (!cookies.isEmpty()){
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
//sync all the cookies in the httpclient with the webview by generating cookie string
for (Cookie cookie : cookies){
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
cookieManager.setCookie(url, cookieString);
CookieSyncManager.getInstance().sync();
}
}
}
//form表单获取url设置同步cookie
webView.setWebViewClient(new WebViewClient() {
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
{
// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
AppConfig.synCookies(QuestionnaireDetailActivity.this,url);
return null;
}
});
相关文章推荐
- Android菜鸟学习笔记(WebView加载html,滑动时闪屏)
- html页面在iOS和Android的webview中不能正确加载的问题
- Android webview 加载html 页面缩放的问题
- android WebView 控件加载本地sdcard中html文件图片的问题
- android webview 加载本地html并且解决多图卡顿问题
- WebView加载Html出现有时页面显示不全问题
- 安卓学习笔记---解决Android签名混淆后WebView与JS交互失效的问题
- Android WebView加载Html右边空白问题的解决方案
- android WebView 控件加载本地sdcard中html文件图片的问题
- 【Android】WebView加载https页面不能正常显示资源问题
- android,使用webView加载页面,界面空隙问题
- 【Android】WebView加载https页面不能正常显示资源问题
- Android WebView加载Html右边空白问题的解决方案
- Android WebView加载出现空白页面问题解决
- android 系列问题记录(webview加载黑屏,页面不跳转,闪退等)
- android webView不能加载本地html文件问题
- Android webview加载Html页面,传参到Html并获取返回值
- android中webview加载html,输入框不能输入内容问题记录
- android中动态加载webview,webview加载html数据,并且隐藏滚动条
- 安卓学习笔记---完美解决Android的WebView加载失败(404,500),显示的自定义视图