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控制。