我需要从Android设备向服务器发送字符串(越南语),如下所示:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constants.URL.UPDATE_CURRENT_STATUS);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("location", "Thạch thất Hanoi "));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int respnseCode = response.getStatusLine().getStatusCode();
if (respnseCode == 200) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
但当服务器得到字符串时,它不像
Thạch thất Hanoi
它变成
Thạch Thất Hanoi
我在服务器端的代码:
@RequestMapping(value = "/UpdateCurrentStatus", method = RequestMethod.POST, produces = { "application/json" })
@ResponseBody
public MessageDTO updateCurrentStatus(
@RequestParam Map<String, String> requestParams) throws TNException {
String location = requestParams.get("location");
System.out.println(location);
MessageDTO result = driverBO.updateCurrentStatus(location);
return result;
}
我该如何解决这个问题?非常感谢。
您是否将android httpclient Content-Type
标头设置为application/json;charset=utf-8而不是"application/json"?
我认为您的问题是您发送的内容实体location
以UTF-8正确编码,但服务器无法确认UTF-8。在"内容类型"标题中对其进行澄清。
您可以使用伟大的http监控工具Fiddler诊断http内容及其标头。
-在下面编辑-
按如下方式放松UrlEncodedFormEntity
。如前所述,将标头设置为application/json; charset=utf-8
。把它设置得静止是件好事。
JSONObject jsonParam = new JSONObject();
jsonParam.put("location", "Thạch thất Hanoi ");
StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8");
httppost.setEntity(entity);