测试Twitter4J的API时,发现在Android上不能显示中文,但是纯JAVA的是可以显示,debug了好久,也没发现哪个地方错了。
通过:br = new BufferedReader(new InputStreamReader(stream, “UTF-8”));读出来的就是UTF8的了,我试着将“UTF-8”去掉,结果还是一样的,中文都变成了下面这个样子:

在android上不能显示中文,还是UTF8格式,很奇怪,但纯JAVA的没问题

接着将它导入到xml解析器中,但奇怪的是在Android上会将中文自动去掉,从中文开始的位置就会自动删掉了。但是纯JAVA是没有问题的,很怪异,不知道这个是不是与平台有关系。

接着搜了下如何将UTF8转成中文:
http://topic.csdn.net/u/20090611/09/b746291c-a2a7-43fb-961b-c37701056d71.html
参考了其中的一段代码。

还要用到正则表达式:
参考这个地方:
http://edu.yesky.com/edupxpt/18/2143018.shtml

于是写出了下面的转换函数:


    /**
     * date:20090826
     * Convert the utf8 to Chinese,If don't, it will not display the Chinese text
     * in the Android Platform correctly.
     *  Use: utf82zh("Twitter4J 在android上不能显示中文,还是UTF8格式,很奇怪,但纯JAVA的没问题"
     * );
     * out:Twitter4J 在android上不能显示中文,还是UTF8格式,很奇怪,但纯JAVA的没问题
     * @author [email protected]
     *
     * @param string The string to converted to Chinese.
     * @return The converted string
     */
    public static String utf82zh(String string) {
        String patternString = "&#([0-9]{3,5});";
        Pattern pp = Pattern.compile(patternString);
        Matcher mm = pp.matcher(string);
        StringBuffer stringBuffer = new StringBuffer();
        while (mm.find()) {
            mm.appendReplacement(stringBuffer, Character.toString((char)Integer.parseInt(mm
                    .group(1), 10)));
        }
        mm.appendTail(stringBuffer);
        string = stringBuffer.toString();
        //System.out.print(string);
        return string;
    }

在需要转换的地方调用它就行了,
在Twitter4J:Reponse.java中,最后一行:

    /**
     * Returns the response body as string.<br />
     * Disconnects the internal HttpURLConnection silently.
     *
     * @return response body
     * @throws TwitterException
     */
    public String asString() throws TwitterException {
        if (null == responseAsString) {
            BufferedReader br;
            try {
                InputStream stream = asStream();
                br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                StringBuffer buf = new StringBuffer();
                String line;
                while (null != (line = br.readLine())) {
                    buf.append(line).append("\n");
                }
                this.responseAsString = buf.toString();
                log(responseAsString);
                stream.close();
                con.disconnect();
                streamConsumed = true;
            } catch (NullPointerException npe) {
                // don't remember in which case npe can be thrown
                throw new TwitterException(npe.getMessage(), npe);
            } catch (IOException ioe) {
                throw new TwitterException(ioe.getMessage(), ioe);
            }
        }
        // It can't display the Chinese in the Android Platform,So I add a
        // function to convert the UTF8 to Chinese
        return utf82zh(responseAsString);
    }

这样在Android上就可以正常显示中文了。

测试时发现问题了,中文引号也不能显示,查了一下,居然是4位数,于是将正则修改为:
String patternString = “&#([0-9]{4,5});”;

Java:UTF8转中文

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.