How to fix commons-codec NoSuchMethodError error.

I download commons-codec-1.11 from here, but in Android have follow error, But on PC is OK.

 I/dalvikvm(27922): Could not find method org.apache.commons.codec.binary.Base64.decodeBase64, referenced from method com.cnr.voicetv.activity.AESCoder.decrypt
W/dalvikvm(27922): VFY: unable to resolve static method 36025: Lorg/apache/commons/codec/binary/Base64;.decodeBase64 (Ljava/lang/String;)[B
D/dalvikvm(27922): VFY: replacing opcode 0x71 at 0x0000
I/dalvikvm(27922): Could not find method org.apache.commons.codec.binary.Base64.encodeBase64String, referenced from method com.cnr.voicetv.activity.AESCoder.encrypt
W/dalvikvm(27922): VFY: unable to resolve static method 36035: Lorg/apache/commons/codec/binary/Base64;.encodeBase64String ([B)Ljava/lang/String;
D/dalvikvm(27922): VFY: replacing opcode 0x71 at 0x000e
I/dalvikvm(27922): Could not find method org.apache.commons.codec.binary.Base64.decodeBase64, referenced from method com.cnr.voicetv.activity.AESCoder.getKey
W/dalvikvm(27922): VFY: unable to resolve static method 36025: Lorg/apache/commons/codec/binary/Base64;.decodeBase64 (Ljava/lang/String;)[B
D/dalvikvm(27922): VFY: replacing opcode 0x71 at 0x0000
I/dalvikvm(27922): Could not find method org.apache.commons.codec.binary.Base64.encodeBase64String, referenced from method com.cnr.voicetv.activity.AESCoder.initKeyString
W/dalvikvm(27922): VFY: unable to resolve static method 36035: Lorg/apache/commons/codec/binary/Base64;.encodeBase64String ([B)Ljava/lang/String;
D/dalvikvm(27922): VFY: replacing opcode 0x71 at 0x0004 
java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64
java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String

Fix

public static byte[] getKey(String key) throws Exception {
        return Base64.decodeBase64(key.getBytes("UTF-8"));//OK
        // error return Base64.decodeBase64(key);
    }
public static String encrypt(String data, String key) throws Exception {
        return StringUtils.newStringUsAscii(Base64.encodeBase64(encrypt(data.getBytes("UTF-8"), getKey(key))));//OK
        //error return Base64.encodeBase64String(encrypt(data.getBytes("UTF-8"), getKey(key)));
    }

猜你喜欢

转载自blog.csdn.net/kingroc/article/details/82050552
今日推荐