簡體   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