[英]Android Studio java.lang.NoSuchMethodError with an imported library
我按照以下步驟導入了commons-codec-1.10.jar:
在我的build.grade中添加了這一行
compile fileTree(dir: 'libs', include: ['*.jar'])
在我的課上我導入了這樣的庫:
import org.apache.commons.codec.binary.Base64;
然后我嘗試在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);
}
}
沒有錯,即使我編譯,除非我運行應用程序,它會拋出以下錯誤,應用程序關閉:
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' appears in /system/framework/org.apache.http.legacy.boot.jar)
順便說一下,我的DoThisThing類不在活動內部,只是為了縮短它。 我檢查了庫,確實是encodeBase64String是靜態的。 所以我不確切知道該怎么做,我是java和android環境中的新手。 所以任何幫助將不勝感激
更換
org.apache.commons.codec.binary.Base64
對於
android.util.Base64
並像這樣更新你的方法。
public static class DoThisThing {
public String DoThisOtherThing() {
String hashed = "hello";
byte[] data = hashed.getBytes("UTF-8");
String hash = Base64.encodeToString(data, Base64.DEFAULT);
return hash;
}
}
好吧,告訴大家,我無法解決這個問題。 我使用原生的android庫進行編碼和解碼,它位於android.util.Base64中
String hash = Base64.encodeToString(hasheado.doFinal(json.getBytes()), Base64.DEFAULT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.