2011-05-26 113 views
2

我現在開發了一個德語應用程序,它將數據從JSON提取到ListView。由於有一些特殊字符,如üöäß,我的代碼如下,這些字符顯示爲對JSON中的特殊字符進行編碼(äöüß)

任何人都可以幫我解決我的問題嗎? THX

public class LooserSync extends IntentService { 

    public LooserSync() { 
     super("LooserSyncService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Database.OpenHelper dbhelper = new Database.OpenHelper(getBaseContext()); 
     SQLiteDatabase db = dbhelper.getWritableDatabase(); 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     db.beginTransaction(); 
     HttpGet request = new HttpGet(
       "http://liebenwald.spendino.net/admanager/dev/android/projects.json"); 
     try { 
      HttpResponse response = httpClient.execute(request); 
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
       InputStream instream = response.getEntity().getContent(); 
       BufferedReader r = new BufferedReader(new InputStreamReader(
         instream), 8000); 
       StringBuilder total = new StringBuilder(); 
       String line; 
       while ((line = r.readLine()) != null) { 
        total.append(line); 
       } 
       instream.close(); 
       String bufstring = total.toString(); 
       JSONArray arr = new JSONArray(bufstring); 
       Database.Tables tab = Database.Tables.AllTables.get(Database.Project.NAME); 
       tab.DeleteAll(db); 
       for (int i = 0; i < arr.length(); i++) { 
        tab.InsertJSON(db, (JSONObject) arr.get(i)); 
       } 
       db.setTransactionSuccessful(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     db.endTransaction(); 
     db.close(); 

    } 

} 

回答

7

JSON內容使用UTF-8作爲默認字符集(請參閱the RFC 4627, chapter 3)。你必須確保該服務器將返回正確的編碼的響應,並明確地使用流讀者UTF-8編碼:

BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8"), 8000); 
+0

謝謝它爲我工作.Laurent Etiemble。 – nikki 2012-08-18 07:35:56

0

這是響應頭您的網址回報:

Accept-Ranges:none 
Connection:Keep-Alive 
Content-Length:14572 
Content-Type:text/plain 
Date:Thu, 26 May 2011 11:47:52 GMT 
ETag:"404f6-38ec-4a42a184b1c80;4a0b4ae611cc0" 
Keep-Alive:timeout=15, max=100 
Last-Modified:Thu, 26 May 2011 09:03:30 GMT 
Server:Apache 

Content-Type頭缺少字符集聲明。如果你可以改變它,那麼它應該工作。

編輯:如果你不能改變這一點,你需要硬編碼一個字符集作爲InputStreamReader的第二個參數。

+0

此外,JSON內容必須爲UTF-8。另見:http://stackoverflow.com/questions/5536140/android-parsing-special-characters-a-o-u-in-json – Olegas 2011-05-26 11:51:23

+0

Olegas是對的。目前它是ISO-8859-1。 – RoToRa 2011-05-26 12:00:23

1

使用下面的代碼來獲取德國數據作爲數據爲UTF-8 STANDRD

HttpGet request = new HttpGet(
       "http://liebenwald.spendino.net/admanager/dev/android/projects.json"); 

httpget.setHeader("charset", "utf-8"); 
     ResponseHandler<String> responseHandler = new ResponseHandler<String>() { 
    public String handleResponse(final HttpResponse response) 
     throws HttpResponseException, IOException { 
     StatusLine statusLine = response.getStatusLine(); 
     if (statusLine.getStatusCode() >= 300) { 
      throw new HttpResponseException(statusLine.getStatusCode(), 
        statusLine.getReasonPhrase()); 
     } 

     HttpEntity entity = response.getEntity(); 
     return entity == null ? null : EntityUtils.toString(entity, "UTF-8"); 
    } 
} 

     String html = httpclient.execute(request, responseHandler); 

感謝 迪帕克

+0

我試圖添加代碼,但導致了一些錯誤 – hectichavana 2011-05-26 13:01:37

2

嘗試顯式定義編碼使用InputStreamReader,例如:

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