您的位置:首页 > 移动开发 > Android开发

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;

}

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: