我有一个文章的应用程序,我正在显示文章在 WebView
. 在Android 9.0版本(API-29)中,这个 WebView
是不工作的。该应用程序显示 什么都没有 在我的文章《活动》中。
mWebView.setVisibility(View.VISIBLE);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setMinimumFontSize(14);
String htmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
+ "<head>"
+ "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"
+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>"
+ "<style type=\"text/css\">body{color: #525252;} img {max-width: 100%; height: auto}</style>"
+ "</head>"
+ item.getContent() //content of item
+ "";
mWebView.loadData(htmlContent, "text/html; charset=utf-8", "UTF-8");
结果,我如何解决这个问题?
我解决了我的问题,这个问题发生在使用最新Chrome浏览器的智能手机上。
解决方法。
不要使用
mWebview.loadData
方法,而不是 使用
mWebview.loadDataWithBaseURL
因此,我的解决方案是。
mWebview.loadDataWithBaseURL(null,htmlContent,"text/html", "utf-8", null);
你的HTML内容应该是Base64或URL编码。你的HTML例子里有一个 "#",这在某些WebView版本上会导致问题。
下面是一个使用Base64编码的例子。
String htmlContent = "...";
String encodedHtml = Base64.encodeToString(htmlContent.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");
这里是 javadoc 以了解详情。
我在使用安卓9.0版本时也遇到了同样的问题。
这个页面的文件(https:/developer.android.comaboutversionspieandroid-9.0-migration。)提到,。
在Android 9中,Java语言的UTF-8解码器更加严格,遵循Unicode标准。
所以我试着把UTF-8转换成Base64,并使用loadData()
try {
String base64 = null;
base64 = android.util.Base64.encodeToString(lecureHtmlData.getBytes("UTF-8"),
android.util.Base64.DEFAULT);
wvLecture.loadData(base64, "text/html; charset=utf-8", "base64");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
现在可以正常工作了。
希望能帮到你