Reputation: 2386
I'm trying to send Japanese characters to my API server but the characters sent were garbled and became ????
. So I set the encoding to the entity using:
StringEntity stringEntity = new StringEntity(message, "UTF-8");
but the output became org.apache.http.entity.StringEntity@4316f850
. I wonder if converting the stringEntity
to string caused this since I want to send it in my server as String
.
Here's how I used it:
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;
}
Where could I have gone wrong?
Upvotes: 3
Views: 2878
Reputation: 3458
You don't need to use StringEntity
.
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("message", message));
Instead, you have to pass a second argument for initializing `UrlEncodedFormEntity.
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value, "UTF-8");
Upvotes: 8