如何在android中打印特殊字符

时间:2012-09-26 13:14:44

标签: android

这是我的网络服务响应..

  

Testperson-se,Approved,Stårgatan1,12345,Ankeborg,SE

现在在Stårgatan的这个字符串中有一个带点的特殊字符,当我打印这个响应时它会给我一个问号字符串...

  

Testperson-se,Approved,St?rgatan 1,12345,Ankeborg,SE

我使用以下代码。

try {
            URL url = new URL(
                    "http://www.ecodor.se/administrator/components/com_ivmstore/ivmwebservices/getaddress.php?pNumber=410321-9202"
                            );// 

            InputSource is = new InputSource(url.openStream());

            StringBuffer res = new StringBuffer();

            res.append(convertStreamToString(is.getByteStream()));

            line = res.toString();

            Log.i("==> responce", line);

            //

        } catch (Exception e) {

        }

public String convertStreamToString(InputStream is) throws IOException {
    /*
     * To convert the InputStream to String we use the
     * BufferedReader.readLine() method. We iterate until the BufferedReader
     * return null which means there's no more data to read. Each line will
     * appended to a StringBuilder and returned as String.
     */
    if (is != null) {
        StringBuilder sb = new StringBuilder();
        String line;

        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "UTF-8"));
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } finally {
            is.close();
        }
        return sb.toString();
    } else {
        return "";
    }
}

如何打印这个特殊字符?

2 个答案:

答案 0 :(得分:1)

您可能希望使用InputSource#getEncoding

返回的编码
  

获取字节流或URI的字符编码。当应用程序提供字符流时,将忽略此值。

答案 1 :(得分:0)

我通过使用以下来解决它。

BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "ISO-8859-1"));

感谢所有能给我理念的人。