utf-8 codepage 在 webView 中

huangapple go评论80阅读模式
英文:

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 -&gt; mWebViewHost.evaluateJavascript(
                &quot;(function() { &quot; +
                        &quot;return (&#39;&lt;html&gt;&#39;+document.getElementsByTagName(&#39;html&#39;)[0].innerHTML+&#39;&lt;/html&gt;&#39;);})();&quot;,

                notify -&gt; Log.d(&quot;mobileSearch&quot;, &quot;notify-&gt;&quot;+notify)));

But i have a problem: I get an incorrect HTML page. It has THE Unicode `D/mobileSearch: notify-&gt;&quot;\u003Chtml&gt;\u003Chead&gt;\n      \u003Cmeta charset=\&quot;utf-8\&quot;&gt;\n\n          \u003Cmeta charset=\&quot;utf-8\&quot;&gt;\n    \u003Cmeta http-equiv=\&quot;X-UA-Compatible\&quot; content=\&quot;IE=edge\&quot;&gt;\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

    &quot;encodeURI(document.getElementsByTagName(&#39;html&#39;)[0].innerHTML)&quot;

then use URLDecoder.

    Log.d(&quot;mobileSearch&quot;, URLDecoder.decode(notify, &quot;UTF-8&quot;))

</details>



huangapple
  • 本文由 发表于 2020年8月3日 22:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63231162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定