简体   繁体   English

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

[英]TextViews with utf-8 and special characters

StackOverflowers! StackOverflowers!

I load some text from a webpage. 我从网页加载了一些文本。 Everything works fine, and shows up in the TextViews. 一切正常,并显示在TextViews中。 But, when the blogs on the webpage have some words with special characters like an: é or something. 但是,当网页上的博客中有一些带有特殊字符的单词时,例如:é或类似的东西。 My Textview show characters like these: Á©.. 我的Textview显示以下字符:Á©..

Can anybody tell what I need to import, call or something like that to show everything tidy? 谁能告诉我需要导入,致电或类似的内容以显示所有内容?

Thanks, 谢谢,

UPDATE 1 + 2: 更新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() is the adres where the loaded text is. current_post.get_intro()是加载的文本所在的地址。 :-) Because I use a listview with many rows.. :-)因为我使用的listview有很多行..

EDIT: 编辑:

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

This is the read/received part. 这是读取/接收的部分。

tricky but worked for me: use Html.fromHtml() twice , I mean: 棘手,但对我Html.fromHtml() :使用Html.fromHtml()两次,我的意思是:

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

EDIT . 编辑

Your problem is not in this TextView because you are getting broken encoding even in your logcat, so you should know when exactly , the encoding is broken ; 您的问题不在此TextView因为即使在logcat中,编码也会被破坏,因此您应该确切知道编码何时被破坏; this get_intro() is returning a bad string, so you should show us , what is this get_intro() doing ? 这个get_intro()返回一个错误的字符串,因此您应该向我们展示,这个get_intro()在做什么? how is it taking the string ? 如何取弦? from what ? 从何而来 ? you should share the code of this get_intro() else nobody can help you... 您应该共享此get_intro()的代码,否则没有人可以帮助您...

Replace 更换

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

With

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

Because DataInputStream.readLine() is deprecated and is discouraged for the following reason: 由于不推荐使用DataInputStream.readLine()因此不建议这样做,原因如下:

This method cannot be trusted to convert bytes to characters correctly. 无法信任此方法将字节正确转换为字符。

BufferedReader also has a readLine method, so the rest of your code should be pretty much unchanged. BufferedReader也具有readLine方法,因此其余代码应保持几乎不变。

Also, whenever you use @SuppressWarnings({ "deprecation" }) I strongly suggest that you be extra careful and make sure you can use the deprecated method despite the warning. 另外,每当您使用@SuppressWarnings({ "deprecation" })我强烈建议您格外小心,并确保即使出现警告也可以使用不推荐使用的方法。

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

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