【问题标题】:Android Encoding for this character set此字符集的 Android 编码
【发布时间】:2014-04-24 04:14:27
【问题描述】:

我正在从网站获取 html 文本。该站点返回如下图所示的字符。我试图从站点找到字符集,它找到了<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

它在文本视图中设置后显示设备上的输出,如:

我尝试了一些编码,但不影响文本,如下所示:

    final Charset windowsCharset = Charset.forName("windows-1252");
    final Charset utfCharset = Charset.forName("UTF-8");
    final CharBuffer windowsEncoded = windowsCharset.decode(ByteBuffer
            .wrap(ne.scrape_detail_article_text.getBytes()));
    final byte[] utfEncoded = utfCharset.encode(windowsEncoded).array();
    // System.out.println(new String(utfEncoded, utfCharset.displayName()));

    String s = "" ;
    try {
        // String s = new String(utfEncoded, utfCharset.displayName());

        //String s = new String(texttoencoding.getBytes("windows-1252"),"UTF-8");

        s = URLEncoder.encode(texttoencoding, "windows-1252");

        Log.e("LOG", "Encoded >> " + s);
    } catch (UnsupportedEncodingException e) {
        Log.e("utf8", "conversion", e);
    }

  TextviewToset.setText(Html.fromHtml(texttoencoding);
    TextviewToset.setMovementMethod(LinkMovementMethod.getInstance());

请帮帮我,我怎样才能将此文本编码为UTF-8 并显示在文本视图中?

提前致谢

【问题讨论】:

    标签: android text utf-8 character-encoding


    【解决方案1】:

    看起来您在这里处理的是 HTML 实体。因此,您必须通过以下方式解码 HTML 实体:

    String text = HTML.fromHtml(yourText).toString();
    

    这将为您提供正确的 UTF-8 字符。 Html.fromHtml() 的文档是 here

    【讨论】:

    • 它对我来说非常好用。显然,在我的测试中,我不得不从输入中跳过 > 。请编辑问题并将网络代码作为文本包含,以便我可以复制它
    • 这是我得到的(第一部分):फेडररलाहरवूनजोकोविचअजिंक्यइंडियनवेल्समास्टर्सटेनिसइंडियनवेल्स,दि。 17(वृत्तसंस्था) - जोकोविचचे हे या वर्षातील पहिले जेतेपदठरले हे व जोकोविचने अंतिम फेरीत फेडररचे आव्हान ३-६, ६-३, (哦,这是网球)
    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2013-11-10
    • 2017-10-23
    • 1970-01-01
    • 2011-03-03
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多