英文:
utf-8 codepage in webView
问题
我尝试保存WebView中的HTML:
```java
button.setOnClickListener(v -> mWebViewHost.evaluateJavascript(
"(function() { " +
"return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');})();",
notify -> Log.d("mobileSearch", "notify->"+notify)));
但我遇到了一个问题:我得到了一个不正确的HTML页面。它包含了Unicode字符 D/mobileSearch: notify->"\u003Chtml\u003Chead>\n \u003Cmeta charset=\"utf-8\">\n\n \u003Cmeta charset=\"utf-8\">\n \u003Cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n
而如果我在浏览器中使用这段代码,它可以正常工作。我该如何修复这个问题?
<details>
<summary>英文:</summary>
I try save the HTML from WebView:
button.setOnClickListener(v -> mWebViewHost.evaluateJavascript(
"(function() { " +
"return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');})();",
notify -> Log.d("mobileSearch", "notify->"+notify)));
But i have a problem: I get an incorrect HTML page. It has THE Unicode `D/mobileSearch: notify->"\u003Chtml>\u003Chead>\n \u003Cmeta charset=\"utf-8\">\n\n \u003Cmeta charset=\"utf-8\">\n \u003Cmeta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n`
And if I use this code in the browser it works correctly. How can I fix it?
</details>
# 答案1
**得分**: 1
Use encodeURI in the javascript function.
for example
"encodeURI(document.getElementsByTagName('html')[0].innerHTML)"
then use URLDecoder.
Log.d("mobileSearch", URLDecoder.decode(notify, "UTF-8"))
<details>
<summary>英文:</summary>
Use encodeURI in the javascript function.
for example
"encodeURI(document.getElementsByTagName('html')[0].innerHTML)"
then use URLDecoder.
Log.d("mobileSearch", URLDecoder.decode(notify, "UTF-8"))
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论