繁体   English   中英

具有utf-8和特殊字符的TextView

[英]TextViews with utf-8 and special characters

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()是加载的文本所在的地址。 :-)因为我使用的listview有很多行..

编辑:

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;
    }

这是读取/接收的部分。

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

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

编辑

您的问题不在此TextView因为即使在logcat中,编码也会被破坏,因此您应该确切知道编码何时被破坏; 这个get_intro()返回一个错误的字符串,因此您应该向我们展示,这个get_intro()在做什么? 如何取弦? 从何而来 ? 您应该共享此get_intro()的代码,否则没有人可以帮助您...

更换

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

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

由于不推荐使用DataInputStream.readLine()因此不建议这样做,原因如下:

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

BufferedReader也具有readLine方法,因此其余代码应保持几乎不变。

另外,每当您使用@SuppressWarnings({ "deprecation" })我强烈建议您格外小心,并确保即使出现警告也可以使用不推荐使用的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM