【发布时间】:2014-02-22 04:43:25
【问题描述】:
我正在尝试将日文字符发送到我的 API 服务器,但发送的字符是乱码并变成了????。所以我使用以下方法将编码设置为实体:
StringEntity stringEntity = new StringEntity(message, "UTF-8");
但是输出变成了org.apache.http.entity.StringEntity@4316f850。我想知道将stringEntity 转换为字符串是否会导致这种情况,因为我想将它作为String 发送到我的服务器中。
这是我的使用方法:
public static String postSendMessage(String path, String message) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); // Timeout limit
HttpPost httpPost = new HttpPost(SystemInfo.getApiUrl() + path);
List<NameValuePair> value = new ArrayList<NameValuePair>();
StringEntity stringEntity = new StringEntity(message, "UTF-8");
value.add(new BasicNameValuePair("message", stringEntity.toString())); //Here's where I converted the stringEntity to string
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
String result = convertStreamToString(is);
return result;
}
我哪里出错了?
【问题讨论】:
标签: android unicode encoding character-encoding http-post