2012-04-18 32 views
0

我在android設備上有httpservice。它對某些瀏覽器形成html頁面的響應。所以它工作正常,但如果我使用其他語言(例如俄語),服務器將返回不可讀的文本,而不是俄語符號。我知道Android上的默認編碼是UTF-8。我已經嘗試了Android上的所有可用編碼(例如windows-1251,Big5(中文),UTF-16等),但它完全返回錯誤結果。下面是一些代碼,告訴你什麼是我試圖做的事:Android沒有合適的編碼

@Override 
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException { 

HttpEntity entity = new EntityTemplate(new ContentProducer() { 
      public void writeTo(final OutputStream outstream) throws IOException { 

       OutputStreamWriter writer = new OutputStreamWriter(outstream, "windows-1251"); 
       String resp = "<html><body>Hello Привет</body></html>"; 
       StringEntity se = new StringEntity(resp, "windows-1251"); 
       se.writeTo(outstream); 
       //writer.write(resp); 
       //writer.flush(); 
      } 
     }); 
    response.setHeader("Context-Type", "text/html");      
    response.setEntity(entity); 
} 

因此,在瀏覽器中我看下:

Hello Привет 

什麼我錯了嗎?請回答我。 我會很感激的任何建議。謝謝。

+0

也許服務器不能正確支持utf-8? – Demonick 2012-04-18 05:54:20

回答

0

您可以嘗試使用java.text.Normalizer或將<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />添加到您的回覆。

此外,我建議你不要硬編碼字符串,但把它們放到資源中,並用Resources.getString(resId)來加載它們 - 因此你的編碼問題可能會更少。

相關問題