WebView.loadData 不适用于 Android 9.0 (API-29)
WebView.loadData not working on Android 9.0 (API-29)
我有一个文章应用程序,我正在 WebView
中显示文章。在 Android 版本 9.0 (API-29) 中,此 WebView
不起作用。该应用在我的文章 Activity 中显示 NOTHING。
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");
请问如何解决这个问题?
您的 HTML 内容应采用 Base64 或 URL 编码。您的 HTML 示例中有一个“#”,它会导致某些 WebView 版本出现问题。
下面是一个使用 Base64 编码的例子。
String htmlContent = "...";
String encodedHtml = Base64.encodeToString(htmlContent.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");
详情请见javadoc。
我在 Android 版本 9.0
上也遇到了同样的问题
本页 (https://developer.android.com/about/versions/pie/android-9.0-migration) 的文档提到:
In Android 9, the UTF-8 decoder for Java language is stricter and
follows the Unicode standard.
所以我尝试将 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();
}
现在一切如常。
希望对您有所帮助
我解决了我的问题,问题出现在具有最新 Chrome 的智能手机中。
解决方案:
不使用
mWebview.loadData
方法,而不是使用
mWebview.loadDataWithBaseURL
因此我的解决方案是:
mWebview.loadDataWithBaseURL(null,htmlContent,"text/html", "utf-8", null);
我有一个文章应用程序,我正在 WebView
中显示文章。在 Android 版本 9.0 (API-29) 中,此 WebView
不起作用。该应用在我的文章 Activity 中显示 NOTHING。
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");
请问如何解决这个问题?
您的 HTML 内容应采用 Base64 或 URL 编码。您的 HTML 示例中有一个“#”,它会导致某些 WebView 版本出现问题。
下面是一个使用 Base64 编码的例子。
String htmlContent = "...";
String encodedHtml = Base64.encodeToString(htmlContent.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");
详情请见javadoc。
我在 Android 版本 9.0
上也遇到了同样的问题本页 (https://developer.android.com/about/versions/pie/android-9.0-migration) 的文档提到:
In Android 9, the UTF-8 decoder for Java language is stricter and follows the Unicode standard.
所以我尝试将 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();
}
现在一切如常。
希望对您有所帮助
我解决了我的问题,问题出现在具有最新 Chrome 的智能手机中。
解决方案:
不使用
mWebview.loadData
方法,而不是使用
mWebview.loadDataWithBaseURL
因此我的解决方案是:
mWebview.loadDataWithBaseURL(null,htmlContent,"text/html", "utf-8", null);