我有一个用于Java和Android项目的公共库,它需要base64编码器/解码器.问题是,Apache commons库不适用于Android,至少不是我能够成功实现 - 由于Android已经实现和早期版本,因此在我尝试编码或解码时在运行时导致错误:
Base64.decodeBase64
Run Code Online (Sandbox Code Playgroud)
返回错误:
AndroidRuntime(1420): java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.decodeBase64
Run Code Online (Sandbox Code Playgroud)
如果有人知道一个兼容Java和Android的base64库,或者可以向我解释如何解决Apache commons问题,我将非常感激.:^)
我按照以下步骤导入了commons-codec-1.10.jar:
在我的build.grade中添加了这一行
compile fileTree(dir: 'libs', include: ['*.jar'])
Run Code Online (Sandbox Code Playgroud)
在我的课上我导入了这样的库:
import org.apache.commons.codec.binary.Base64;
Run Code Online (Sandbox Code Playgroud)
然后我尝试在Base64中访问encodeBase64String静态方法,如下所示:
public static class DoThisThing {
public String DoThisOtherThing() {
String hashed = "hello";
String hash = Base64.encodeBase64String(hashed.getBytes());
return hash;
}
}
public class ActivityThing extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_thing);
String hash = DoThisThing.DoThisOtherThing();
System.out.println(hash);
}
}
Run Code Online (Sandbox Code Playgroud)
没有错,即使我编译,除非我运行应用程序,它会抛出以下错误,应用程序关闭:
11-03 09:41:27.719 2390-2476/com.myproject E/AndroidRuntime: Caused by: java.lang.NoSuchMethodError: 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' …Run Code Online (Sandbox Code Playgroud) 我从Webview发布到https服务器,如下面的URL所示,BASE64为charset.
我的postdata字符串是Base64编码的字符串,其中包含"+".
当我按上述URL中所示的方式发布时,服务器日志显示缺少"+"的postdata字符串
我应该能够从Webview发布任何数据字符串,因为我将发布一个我无法控制的Base64编码字符串.
请帮我解决这个问题.
更新:我甚至尝试过这样的
String postData = "fileContents=" + fileCon;
mWebView.postUrl(url,postData.getBytes());
Run Code Online (Sandbox Code Playgroud)
但发布时仍然会从postData中删除"+".如果postData中没有"+",则会正确发布.