Ir_rev
Ir_rev

Reputation: 13

utf-8 codepage in webView

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?

Upvotes: 1

Views: 176

Answers (1)

Viatcheslav Ehrmann
Viatcheslav Ehrmann

Reputation: 746

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"))

Upvotes: 1

Related Questions