【问题标题】:UTF-8 not encoding html in webview androidUTF-8 未在 webview android 中编码 html
【发布时间】:2013-01-23 07:09:18
【问题描述】:

我正在使用以下代码对我的资产文件夹中的 html 文件进行编码。 我在这里浏览了各种链接,但没有成功。 这是我的一段代码。

 WebSettings settings = myWebVeiw.getSettings();
    //settings.setJavaScriptEnabled(true);
    //myWebVeiw.getSettings().setJavaScriptEnabled(true);
    settings.setDefaultTextEncodingName("utf-8");
    //settings.setDefaultTextEncodingName("ISO-8859-1");

    myWebVeiw.loadUrl("file:///android_asset/"+totaldays+".html"); 

虽然它适用于其他字符,但它无法编码 - 因为它在 web 视图上打印相同。 请建议我该怎么做。

任何帮助将不胜感激。

【问题讨论】:

    标签: android encoding


    【解决方案1】:

    您需要将 WebSettings 默认文本编码设置为 utf-8。并在将 html 标头字符集强制为 utf-8 之后。示例:

    String htmlText = "<html> your page here </html>";
    WebView web = (WebView) findViewById(R.id.webview); // get your webview from the layout
    WebSettings settings = web.getSettings();
    settings.setDefaultTextEncodingName("utf-8");
    web.loadData(htmlText, "text/html; charset=utf-8", "utf-8");
    

    这适用于我在 android 2.2.1、4.0.4 和 4.1.2 上。

    【讨论】:

    • with "text/html; charset=utf-8", "utf-8" 为我工作。谢谢。 (安卓5.0.1)
    • "text/html; charset=utf-8" 是我的主要求解器。谢谢!
    【解决方案2】:

    你可以试试看:

    myWebVeiw.loadDataWithBaseURL("file:///android_asset/"+totaldays+".html", null, "text/html", "utf-8",null);
    

    【讨论】:

    • 是的,我已经尝试过,但它只显示一个空白页。这可能是html可能没有正确解码的原因吗?
    【解决方案3】:

    对 webView 不太熟悉。 但是浏览器会解析以下 html 字符集编码: 1.first charset .so请检查totaldays.html &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; 2.根据内容。

    【讨论】:

    • Meta 对我没有帮助。因为它是静态 html 我猜所以我已经尝试过了..等待客户端。我想这可能是客户端的 html 解码错误,这就是为什么不在 android 端编码。
    猜你喜欢
    • 2012-09-22
    • 2012-01-24
    • 2014-02-22
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多