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

Android WebView常见问题解决方案汇总

2015-08-24 18:31 561 查看

1.webview加载中文乱码

做项目的时候,需要显示网页上提取的一段HTML片段,使用loadData乱码,后来发现使用loadDataWithBaseURL没有这个问题。

// webview_cmpt_detail.loadData(contentHtml.toString(), "text/html","utf-8");此方法显示中文乱码
webview_cmpt_detail.loadDataWithBaseURL(null, contentHtml.toString(), "text/html","utf-8", null);

2.添加LocalStorage存储

默认WebView没有开启LocalStorage存储。开启方法如下

// 开启DOM缓存。
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCacheMaxSize(1024*1024*8);
webSettings.setDatabasePath(this.getCacheDir().getAbsolutePath()); 注:setDatabasePath在API19时已经废弃,原因是因为在4.4WebView的内核已经换为了Chrome的内核,存储路径有WebView控制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: