Creating a new line from Charset raises NoSuchMethodError (Android)

I have a piece of code to create a new String as follows:

 private final static Charset UNICODE_CHARSET = Charset.forName("UTF-8"); public String makeNewUnicodeString(byte[] octects) { return new String(octects, UNICODE_CHARSET); } 

It works fine when testing on my computer. But when I run it on the Android emulator, it produces:

 java.lang.NoSuchMethodError: java.lang.String.<init> 

But it works:

 public String makeUnicodeString(byte[] octets) { try { return new String(octets, "UTF-8") } catch (UnsupportedEncodingException uee) { // never throw. } } 

I am using Android 2.2 API 8, ed. 2.

+6
source share
3 answers

Since the constructor String (byte[] data, Charset charset) was added only in the API level 9 (Android SDK 2.3). Therefore, updating the SDK version solved my problems. Thanks to everyone.

Here is the link:

String - Android Developer Reference.

Android platform API levels .

+4
source

This is similar to the difference between Java 5 and Java 6.

The constructor that accepts CharSet is only in Java 6, not Java 5.

http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html

http://download.oracle.com/javase/6/docs/api/java/lang/String.html

EDIT - this constructor is in android api .. this does not answer the question.

+1
source

You can use this as

 byte[] raw = null; try { raw = key.getBytes("US-ASCII"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/892197/


All Articles