StandardCharsets.UTF_8 on lower API lower than 19

EspeH picture EspeH · Aug 19, 2015 · Viewed 14.8k times · Source

I'm using this code to get scandinavian characters posted to php correctly.

Issue here is that StandardCharsets.UTF_8 is not supported before API 19

byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

DataOutputStream wr = new DataOutputStream( con.getOutputStream());
wr.write( postData );

Field requires API level 19 (Current min is 14): java.nio.charset.StandardCharsets#UTF_8

How should I do this with API lower than 19?

Answer

Kirill Feoktistov picture Kirill Feoktistov · Sep 7, 2015

Use forName static method of Charset class:

byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8"));

List of standard charsets you can find in documentation.