【问题标题】:TextViews with utf-8 and special characters带有 utf-8 和特殊字符的 TextViews
【发布时间】:2014-06-15 14:27:12
【问题描述】:

StackOverflowers!

我从网页加载了一些文本。一切正常,并显示在 TextViews 中。但是,当网页上的博客有一些带有特殊字符的单词时,例如:é 或其他东西。我的 Textview 显示如下字符:Á©..

谁能告诉我需要导入、调用或类似的东西来显示一切整洁?

谢谢,

更新 1 + 2:

TextView intro_text = (TextView) item_view.findViewById(R.id.item_intro);
intro_text.setText(Html.fromHtml(current_post.get_intro()));    

current_post.get_intro() 是加载文本的地址。 :-) 因为我使用了很多行的列表视图..

编辑:

public class connectTask extends AsyncTask<String, String, String> {
    @SuppressWarnings({ "deprecation" })
    @Override
    protected String doInBackground(String... message) {

        Log.v("Correct Load", "Starting ");
        URL u;
        InputStream is = null;
        DataInputStream dis;
        String s;

        try {
            Log.v("Connecting...");
            u = new URL("http://.......");
            is = u.openStream();
            dis = new DataInputStream(new BufferedInputStream(is));
            Log.v("Connected");

            try {
                while ((s = dis.readLine()) != null) {
                    if (s.contains("post_wrapper")) {
                        for (int i = 0; i < j; i++) {
                            while ((s = dis.readLine()) != null) {
                                if (s.contains("post_intro")) {
                                    break;
                                }
                            }

                            if (s != null) {
                                s = dis.readLine();
                                Log.v("Intro", s); intro[i] = s.substring(s.indexOf("<p>") + 3, s.indexOf("</p>"));
                                Log.e("Intro", "Found intro:" + intro[i]);
                            }   
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException mue) {
            System.out.println("Ouch - a MalformedURLException happened.");
            mue.printStackTrace();
            System.exit(1);

        } catch (IOException ioe) {
            System.out.println("Oops- an IOException happened.");
            ioe.printStackTrace();
            System.exit(1);

        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException {
            }
        }
        return null;
    }

这是读取/接收的部分。

【问题讨论】:

  • 这是一个编码问题。您的 TextViews 不使用与您的 WebService 相同的编码(希望它使用 UTF-8)。
  • 我最近也遇到了这个问题。事实证明,HTTP 请求和响应的默认编码是 ISO-8859-1,它将这些特殊字符编码为单个字符,但 UTF-8 将它们编码为双字符。所以当 UTF-8 被解码为 ISO-8859-1 时,你最终会得到多个不同的字符(通常带有 Á)
  • @Bob Malooga 是的,在我的布局 xml 之上,我有那一行..
  • @Dan Temple 你到底是什么意思?我需要做什么/尝试什么?
  • 好吧,我不太确定它如何应用于 Android,但您可以随时尝试:text.setTest(Html.fromHtml( URLDecoder.decode( stringVariable, "UTF-8"); ).toString());

标签: java android html utf-8 textview


【解决方案1】:

替换

DataInputStream dis = new DataInputStream(new BufferedInputStream(is));

BufferedReader dis = new BufferedReader(new InputStreamReader(is, "UTF-8"));

因为DataInputStream.readLine() 已被弃用且不鼓励使用,原因如下:

不能信任此方法将字节正确转换为字符。

BufferedReader 也有一个 readLine 方法,所以你的其余代码应该几乎没有改变。

此外,无论何时使用@SuppressWarnings({ "deprecation" }),我强烈建议您格外小心,并确保尽管出现警告,您仍可以使用已弃用的方法。

【讨论】:

    【解决方案2】:

    棘手但对我有用:使用 Html.fromHtml() 两次,我的意思是:

    text.setText(Html.fromHtml(Html.fromHtml(your_html_text).toString()));
    

    编辑

    您的问题不在此TextView 中,因为即使在您的 logcat 中,您的编码也会被破坏, 所以你应该知道什么时候编码被破坏了;这个get_intro() 正在返回一个错误的字符串,所以你应该告诉我们,这个get_intro() 在做什么?它是如何使用字符串的?从何而来 ?你应该分享这个get_intro()的代码,否则没有人可以帮助你......

    【讨论】:

    • 得到错误:Html类型中的fromHtml(String)方法不适用于参数(Spanned)。
    • 没有错误了,但我仍然有奇怪的Á和©字符..而不是:é。
    • 当你记录你想要显示的字符串时,你是如何得到它的?因为如果您必须验证您在写入或读取字符串时是否有问题。做Log.i("test",your_html_text); 看看你得到了什么字符串...
    • 我的日志也不好!日志显示相同的奇怪字符。
    • 然后向我们展示您用于从页面或 Web 服务读取文本的代码...您的问题与 TextView 无关...
    猜你喜欢
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多