2011-04-28 17 views
1

我有一個問題,我似乎無法自己弄清楚。特殊字符構成REST web服務通信問題

我想從Android應用程序發佈JSON對象到REST web服務。一切工作正常,直到我添加像å,ä,ö這樣的特殊字符。

JSONObject absenceObject = new JSONObject(); 
absenceObject.put(INFO_DESCRIPTION, "åka pendeltåg"); 
StringEntity entity = new StringEntity(absenceObject.toString()); 
httpPost.setEntity(entity); 
httpPost.setHeader("Accept", "application/json";character); 
httpPost.setHeader("Content-type", "application/json;charset=UTF-8"); 
HttpResponse response = httpclient.execute(httpPost); 

如果我打印absenceObject.toString()並將結果複製到常規休息客戶端,它工作正常。

我非常感謝任何幫助!

回答

9

嘗試指定所需的字符集在StringEntity構造:

StringEntity entity = new StringEntity(absenceObject.toString(), "UTF-8"); 
+0

非常感謝! – notBanana 2011-04-28 13:52:28

+0

StringEntity entity = new StringEntity(body,「UTF-8」); //wr.writeBytes(entity.toString()); wr.writeUTF(entity.toString());我曾嘗試兩種方式,但沒有工作。提前致謝。 – 2016-04-13 06:04:08

0

回覆:馬克的響應

嘗試在StringEntity構造函數中指定所需的字符集:

StringEntity entity = new StringEntity(absenceObject.toString(), "UTF-8");

注意構造之後設置的字符集,我沒有工作,即

entity.setContentEncoding("UTF-8"); 

我不得不做的馬克說,並設置它在構造函數中。

邁克爾

0
byte[] buf = body.getBytes(HTTP.UTF_8); 
wr.write(buf, 0, buf.length); 

試試這個它會工作。