Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons Codec with Android: could not find method

Today I tried including the apache.commons.codec package in my Android application and couldn't get it running. Android could not find method ord.apache.commons.codec.binary.* and output the following errors in DDMS

01-12 08:41:48.161: ERROR/dalvikvm(457): Could not find method org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString, referenced from method com.dqminh.app.util.Util.sendRequest

01-12 08:41:48.161: WARN/dalvikvm(457): VFY: unable to resolve static method 10146: Lorg/apache/commons/codec/binary/Base64;.encodeBase64URLSafeString ([B)Ljava/lang/String;

01-12 08:41:48.161: WARN/dalvikvm(457): VFY: rejecting opcode 0x71 at 0x0004

Any clue on how to solve this problem ? Thanks a lot.

like image 237
dqminh Avatar asked Jan 12 '10 08:01

dqminh


2 Answers

I had a similar problem while using android with an OAuth library I'm developing.

I also got from android that, although I had included apache.commons.codec in the classpath, a particular method (encodeBase64String) was not found.

Checking the javadocs, both methods claim to be 1.4 and greater only, so my guess is that android already includes an older version of commons.codec where these methods are indeed undefined.

My solution was to use an older method, like this:

String encodedString = new String(Base64.encodeBase64('string to encode')); 

The method you want to use is different since it replaces + and / with url-safe values - and _. So you probably might use something like:

String encodedString = new String(Base64.encodeBase64('string to encode')); String safeString = encodedString.replace('+','-').replace('/','_'); 

Hope that helps!

like image 53
Pablo Fernandez Avatar answered Sep 21 '22 10:09

Pablo Fernandez


You don't have to use apache commons, on android you can use android.util.Base64 instead.

like image 38
Mootaz Dinana Avatar answered Sep 21 '22 10:09

Mootaz Dinana