简体   繁体   中英

Android studio java.lang.NoSuchMethodError: No static method encodeBase64URLSafeString([B)Ljava/lang/String;

I'm trying to use Upbit API to call my wallet information.

sample code is from https://docs.upbit.com/reference#%EC%9E%90%EC%82%B0-%EC%A1%B0%ED%9A%8C

This is my code.

(erased my API access key and secret key)


package gachon.mpclass.apitest2;

import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.util.UUID;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;


import android.util.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NetworkThread thread = new NetworkThread();
        thread.start();
    }


    class NetworkThread extends Thread {

        public void run() {


            try {

                String accessKey = ("api key value");
                String secretKey = ("api key value");
                String serverUrl = ("https://api.upbit.com");


                Algorithm algorithm = Algorithm.HMAC256(secretKey);
                String jwtToken = JWT.create()
                        .withClaim("access_key", accessKey)
                        .withClaim("nonce", UUID.randomUUID().toString())
                        .sign(algorithm);

                String authenticationToken = "Bearer " + jwtToken;
                HttpClient client = HttpClientBuilder.create().build();
                HttpGet request = new HttpGet(serverUrl + "/v1/accounts");
                request.setHeader("Content-Type", "application/json");
                request.addHeader("Authorization", authenticationToken);

                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();


                System.out.println(EntityUtils.toString(entity, "UTF-8"));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}

SDK Version is 30 and dependencies are

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation fileTree(dir: 'C:\\Users\\admin\\AndroidStudioProjects\\APITest2\\app\\libs', include: ['*.aar', '*.jar'], exclude: [])
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    compileOnly 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    implementation group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
    implementation group: 'commons-codec', name: 'commons-codec', version: '1.10'
    implementation 'com.auth0.android:jwtdecode:2.0.0'
    api 'io.jsonwebtoken:jjwt-api:0.11.2'
    runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
    runtimeOnly('io.jsonwebtoken:jjwt-orgjson:0.11.2') { exclude group: 'org.json', module: 'json'}
    runtimeOnly 'org.bouncycastle:bcprov-jdk15on:1.60'
    implementation 'com.android.support:multidex:1.0.3'

}

and error is

E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: gachon.mpclass.apitest2, PID: 21107
    java.lang.NoSuchMethodError: No static method encodeBase64URLSafeString([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.jar)
        at com.auth0.jwt.JWTCreator.sign(JWTCreator.java:283)
        at com.auth0.jwt.JWTCreator.access$100(JWTCreator.java:23)
        at com.auth0.jwt.JWTCreator$Builder.sign(JWTCreator.java:264)
        at gachon.mpclass.apitest2.MainActivity$NetworkThread.run(MainActivity.java:48)

I tried lots of libraries and dependencies but still same error occurs. Should I change entire code or is there any working dependencies?

This error usually occurs when the wrong version of a jar is on the classpath, in this case it might be that the application is looking for the Base64 class in the follow jar /system/framework/org.apache.http.legacy.jar . Take a look at this commit https://github.com/auth0/java-jwt/commit/7dea6ac54d5b5b8822a9f3ee41cc4666e250cc27#diff-66fdd511fc2a34e115ac3e635c97eb82b89c8b64e26ea09247712b9efa5e62f7

Looking at the auth0.jwt project you can see where Base64.encodeBase64URLSafeString(byte []) method is called inside the JWTCreator.sign() method, an error is likely thrown because the version of the Base64 class on your classpath does not contain that function. It would be helpful if you posted your entire build.gradle file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM