Search code examples
javaandroidhttpencodingurl-parameters

StandardCharsets.UTF_8 on lower API lower than 19


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?


Solution

  • Use forName static method of Charset class:

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

    List of standard charsets you can find in documentation.