2012-09-26 92 views
2

這裏是我的web服務響應..如何打印特殊字符的Android

Testperson-SE,批准,Stårgatan1,12345,Ankeborg,SE

在此字符串

現在在Stårgatan一個帶點特殊字符的是那裏當我打印該響應它給我的問號字符串如..

Testperson-SE,批准,聖?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 ""; 
    } 
} 

如何打印此特殊字符?

+0

看來你正確地讀取數據(假設輸入真的是UTF-8編碼)。可能是因爲終端無法顯示UTF-8? (您是否使用Windows cmd?) – nhahtdh

+0

請看這個鏈接 - http://code.google.com/p/android/issues/detail?id=507。在這個鏈接檢查評論4由p ... @ hotmail.com,2008年8月25日 –

回答

0

我用下面的方法解決它。

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

感謝所有給我的想法。

1

您可能需要使用恢復編碼由InputSource#getEncoding

獲取字節流或URI的字符編碼。當應用程序提供字符流時,該值將被忽略。