为越南Android设置UTF-8

时间:2015-03-05 17:13:22

标签: android string utf-8 server

我需要从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;

}

如何解决此问题?谢谢。

1 个答案:

答案 0 :(得分:0)

您是否将android httpclient Content-Type标头设置为 application / json; charset = utf-8 而不是“application / json”?

我认为您的问题是您发送的内容实体location在UTF-8中正确编码,但服务器无法确认UTF-8。在Content-Type标题中澄清它。

您可以使用优秀的http监控工具Fiddler来诊断http内容及其标题。

-EDIT BELOW -

如下所示取消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);