Sunday, April 28, 2019

Android: No static method encodeBase64String in org.apache.commons.codec.binary.Base64


When I used org.apache.commons.codec.binary.Base64.encodeBase64String in my Android application, this exception was thrown:

No static method encodeBase64String([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' appears in /system/framework/org.apache.http.legacy.boot.jar

This method is available since version 1.4 of the Apache Commons Codec. If the OS of the phone has only an older version of the Codec package, the method won't be available. Alternatively, we can use a method that exists in older versions.

Instead of:
String encodedString = Base64.encodeBase64String(bytes);

Use:
String encodedString = new String(Base64.encodeBase64(bytes));

And for decoding, instead of:
byte[] bytes = Base64.decodeBase64(encodedString);

Use:
byte[] bytes = Base64.decodeBase64(encodedString.getBytes());


No comments:

 
Get This <