带有 Android 的 Apache Commons Codec:找不到方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2047706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:21:48  来源:igfitidea点击:

Apache Commons Codec with Android: could not find method

androidbase64apache-commons-codec

提问by dqminh

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

今天我尝试在我的 Android 应用程序中包含 apache.commons.codec 包,但无法运行。Android 找不到方法 ord.apache.commons.codec.binary.* 并在 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

01-12 08:41:48.161: 错误/dalvikvm(457): 找不到方法 org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString,从方法 com.dqminh.app.util.Util.sendRequest 引用

01-12 08:41:48.161: WARN/dalvikvm(457): VFY: 无法解析静态方法 10146: Lorg/apache/commons/codec/binary/Base64;.encodeBase64URLSafeString ([B)Ljava/lang/String;

01-12 08:41:48.161: WARN/dalvikvm(457): VFY: 在 0x0004 拒绝操作码 0x71

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

关于如何解决这个问题的任何线索?非常感谢。

回答by Pablo Fernandez

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

我在使用 android 和我正在开发的OAuth 库时遇到了类似的问题。

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

我也从 android 那里得到,虽然我已经包含apache.commons.codec在类路径中,encodeBase64String但没有找到特定的方法 ( )。

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.codecwhere these methods are indeed undefined.

检查 javadocs,这两种方法都声称只有1.4 及更高版本,所以我的猜测是 android 已经包含了commons.codec这些方法确实未定义的旧版本。

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:

您要使用的方法是不同的,因为它将 + 和 / 替换为 url 安全值 - 和 _。所以你可能会使用类似的东西:

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

Hope that helps!

希望有帮助!

回答by Mootaz Dinana

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

您不必使用 apache commons,在 android 上您可以使用android.util.Base64代替。

回答by zarthross

I experienced the exact same problem. So i started browsing the android source code, and as it turns out Pablo Fernandez's guess about Android having an implementation of org.apache.commons.code.binary is correct. However, its version 1.2 of the apache commons, not version 1.4 or even 1.5. You can see for your self in the android source.

我遇到了完全相同的问题。所以我开始浏览 android 源代码,结果证明 Pablo Fernandez 关于 Android 有 org.apache.commons.code.binary 实现的猜测是正确的。但是,它的 apache commons 是 1.2 版,而不是 1.4 版甚至 1.5 版。您可以在 android源代码中自行查看。

as a note this is question is a duplicate of this post

请注意,这是问题是这篇文章的副本

回答by RWIL

You can use the following function:

您可以使用以下功能:

private static String encodeBase64URLSafeString(byte[] binaryData) {

        return android.util.Base64.encodeToString(binaryData, Base64.URL_SAFE);

    }

source + listing of other possible flags: http://developer.android.com/reference/android/util/Base64.html

源 + 其他可能标志的列表:http: //developer.android.com/reference/android/util/Base64.html

回答by Sverrir Sigmundarson

You could simply copy this code bit from the apache library (it is pretty isolated) http://www.java2s.com/Open-Source/Android/Blog-Twitter/twitspeak/org/apache/commons/codec/binary/Base64.java.htm

您可以简单地从 apache 库中复制此代码位(它非常孤立) http://www.java2s.com/Open-Source/Android/Blog-Twitter/twitspeak/org/apache/commons/codec/binary/Base64 .java.htm

Edit:Updated link from the Wayback Machine as the original is gone: http://web.archive.org/web/20130610025148/http://www.java2s.com/Open-Source/Android/Blog-Twitter/twitspeak/org/apache/commons/codec/binary/Base64.java.htm

编辑:来自 Wayback Machine 的更新链接,因为原始链接已经消失:http://web.archive.org/web/20130610025148/http: //www.java2s.com/Open-Source/Android/Blog-Twitter/twitspeak/ org/apache/commons/codec/binary/Base64.java.htm

回答by R. Simac

My solution to the same problem was to rename the problematic class org.apache.commons.codec.binary.Base64.javainto org.apache.commons.codec.binary.ApacheBase64.java. I did it using Eclipse refactor-rename.

我对同一问题的解决方案是将有问题的类重命名org.apache.commons.codec.binary.Base64.javaorg.apache.commons.codec.binary.ApacheBase64.java. 我使用 Eclipse refactor-rename 做到了。

That way, the latest and greatest apache solution is preserved and used, and there is no chance for accidental problem recurrence when my app is eventually being lifted from android 1.6 lowest denominator.

这样,最新和最好的 apache 解决方案被保留和使用,并且当我的应用程序最终从 android 1.6 最低分母中解除时,不会有意外问题再次发生的机会。

Note I had the entire apache commons source tree already set as separate Eclipse java project, next to my Android project. My Android project used many of Apache Commons classes, but failed on Base64 because of above described problems...

注意我已经将整个 apache commons 源树设置为单独的 Eclipse java 项目,在我的 Android 项目旁边。我的 Android 项目使用了许多 Apache Commons 类,但由于上述问题在 Base64 上失败了......

回答by Martin Pfeffer

Please note that this answer was made by Dylan Watsonin the comments above:

请注意,这个答案是由Dylan Watson在上面的评论中提出的:

Be aware that you need to use Base64.encode("foobar".getBytes(), Base64.Base64.NO_WRAP); to get exactly the same result as the apache commons library. See: stackoverflow.com/questions/17912119

请注意,您需要使用 Base64.encode("foobar".getBytes(), Base64.Base64.NO_WRAP); 获得与 apache commons 库完全相同的结果。请参阅:stackoverflow.com/questions/17912119

This answer was the only which worked after hours of trying to communicate my android app with a java "desktop" app.

这个答案是在尝试将我的 android 应用程序与 java“桌面”应用程序通信数小时后唯一有效的答案。

So here is some source code, maybe it will help others:

所以这里有一些源代码,也许它会帮助别人:

Code from the desktop applet:

来自桌面小程序的代码:

   private static String convertToBase64(String s) {
        byte[] bytes = new byte[0];
        try {
            bytes = (s.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DatatypeConverter.printBase64Binary(bytes);
    }

This snippet is used in the android app:

此代码段用于android 应用程序

 public static String convertToBase64(String password) {

        byte[] bPwd = new byte[0];
        try {
            bPwd = (password.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            password = new String(Base64.encode(bPwd, Base64.NO_WRAP), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return password;
    }

回答by user3125139

did you include apache commons project lib,like

你有没有包括 apache commons 项目库,比如

org.apache.commons:commons-compress
org.apache.commons:commons-email
org.apache.commons:commons-io
org.apache.commons:commons-lang3
org.apache.commons:commons-parent
org.apache.commons:commons-pool2

the commons-codec 's GAV is

公共编解码器的 GAV 是

commons-codec:commons-codec 

but it's package name is

但它的包名是

org.apache.commons.codec

this package name will be conflict with apache commons project libs,try to change the package name of commons-codec,then generate it as a jar or import the source code had changed the package name;

这个包名会和apache的commons项目libs冲突,尝试修改commons-codec的包名,然后生成jar或者导入改过包名的源码;

回答by pringi

Knowing that this is an old question, but I faced this problem recently when compiling for API Level 23, and found another solution for the problem: use guavaproject.

知道这是个老问题,但是最近在编译API Level 23时遇到了这个问题,找到了另一个解决方法:使用guava项目。

// Encoding
String encodedString = BaseEncoding.base64().encode(text.getBytes(UTF_8));
System.out.println("encodedString: " + encodedString);

// Decoding
byte[] decodedString = BaseEncoding.base64().decode(encodedString);
System.out.println("decodedString: " + new String(decodedString, UTF_8));

The guava library (18.0) was the version that I've used, and the change was smooth. The code works as expected.

番石榴库 ( 18.0) 是我使用的版本,并且更改很顺利。该代码按预期工作。

Solution found in here: https://memorynotfound.com/encode-decode-base64-string-using-native-java/

在这里找到的解决方案:https: //memorynotfound.com/encode-decode-base64-string-using-native-java/

回答by Vicky Kapadia

This works for me:

这对我有用:

import org.apache.commons.codec.binary.Base64;

/**
 * This is used to get the base64 from the bytes
 *
 * @param rawBytes the raw bytes
 * @return the base64 encoded string
 */
public static String getBase64String(byte[] rawBytes) {
    String base64Str = "";
    if (rawBytes!= null) {
        base64Str= new String(Base64.encodeBase64(rawBytes));
    }
    return base64Str;
}

public static byte[] getBase64DecodedString(String base64EncodedStr) {
    byte[] base64Bytes = Base64.decodeBase64(base64EncodedStr.getBytes());
    return bitmap;
}