Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 636
EspeH Avatar asked Aug 19 '15 17:08

EspeH


People also ask

What is Standardcharsets utf_8 in Java?

Introduction. When working with Strings in Java, we oftentimes need to encode them to a specific charset, such as UTF-8. UTF-8 represents a variable-width character encoding that uses between one and four eight-bit bytes to represent all valid Unicode code points.

What is Standardcharsets Us_ascii?

US_ASCII. public static final Charset US_ASCII. Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.


1 Answers

Use forName static method of Charset class:

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

List of standard charsets you can find in documentation.

like image 141
Kirill Feoktistov Avatar answered Sep 30 '22 20:09

Kirill Feoktistov