in Android Code Tips

UTF-8 encoding at post values with HttpClient

It took me a while to find out how, so I believe it will be useful for you too.

HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://urlhere.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name1", "utfvalue1"));
nameValuePairs.add(new BasicNameValuePair("name2", "utfvalue2"));
UrlEncodedFormEntity formEntity = null;
try {
	formEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
} catch (UnsupportedEncodingException e2) {
	e2.printStackTrace();
}
if (formEntity != null)
	httpPost.setEntity(formEntity);

...
Share