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

Convert text file from ISO-8859-7 to UTF-8

As a linux user, I often receive documents in ISO-8859-7 from Windows users.

There is a great unix tool to convert the encodings and it’s called iconv. Here how to use it:

iconv --from-code=ISO-8859-7 --to-code=UTF-8 original.txt > converted.txt

Share

WebView not displaying UTF-8 Data

If you have a WebView and you want to display html code, you will probably use the WebView.loadData function as seen in the example here.

String htmlCode = "<html><body><b>hello man!</b> non bold text</body></html>";
mWebView.loadData(htmlCode, "text/html", "UTF-8");

Done by the book, nice try, though it does not always work.
When the html code contains UTF data, you have to base encode all the html data.
You can accomplish that by using the loadDataWithBaseURL function.
example:

String htmlCode = "<html><body><b>hello man!</b> non bold text</body></html>";
mWebView.loadDataWithBaseURL(null, htmlCode, "text/html", "UTF-8", null);

Ready :)

Share