OkHttp NetBeans NoClassDefFoundError

OkHttp NetBeans NoClassDefFoundError

我有一个 Android 应用程序,它使用 OkHttp 进行网络速度测试。现在我需要使用 NetBeans 8.2 为桌面创建另一个客户端,但我无法使 OkHttp 工作。我包括库 okhttp-3.11.0 和 okio-2.0.0,但我得到 java.lang.NoClassDefFoundError.

我的代码是:

public class TestCheckNetwork {

private final JFrame frame;
private final OkHttpClient client;

long startTime;
long endTime;
long fileSize;

TestCheckNetwork(JFrame frame) {
    this.frame = frame;
    client = new OkHttpClient();
}

void initTest() {
    Request request = new Request.Builder().url("http://ipv4.ikoula.testdebit.info/50k.iso").build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
            Utils.speedNetwork = 0;
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            startTime = System.currentTimeMillis();

            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            Headers responseHeaders = response.headers();
            for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                //TODO log
            }

            InputStream input = response.body().byteStream();

            try {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];

                while ((System.currentTimeMillis() - startTime) < 50000 && input.read(buffer) != -1) {
                    bos.write(buffer);
                }
                byte[] docBuffer = bos.toByteArray();
                fileSize = bos.size();
                bos.close();

            } finally {
                input.close();
            }

            endTime = System.currentTimeMillis();

            // calculate how long it took by subtracting endtime from starttime
            double timeTakenMills = Math.floor(endTime - startTime);  // time taken in milliseconds
            final double timeTakenSecs = timeTakenMills / 1000;  // divide by 1000 to get time in seconds
            final int kilobytePerSec = (int) Math.round(1024 / timeTakenSecs);


            // get the download speed by dividing the file size by time taken to download
            final double speed = fileSize / timeTakenMills;
            Utils.speedNetwork = (int) speed;



        }
    });
}
}

得到的错误是:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: kotlin/TypeCastException
at okhttp3.ResponseBody.create(ResponseBody.java:210)
at okhttp3.internal.Util.<clinit>(Util.java:60)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:123)
at assistantremote.TestCheckNetwork.<init>(TestCheckNetwork.java:35)
at assistantremote.AssistantRemote.inicializate(AssistantRemote.java:119)
at assistantremote.AssistantRemote.<init>(AssistantRemote.java:81)
at assistantremote.AssistantRemote.lambda$main[=11=](AssistantRemote.java:1758)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: kotlin.TypeCastException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 21 more
BUILD STOPPED (total time: 6 seconds)

我只找到了 Maven 或 Android 的解决方案。我的错误在哪里或者没有 OkHttp 我怎么能犯错?谢谢

尝试将 kotlin stdlib 和 stdlib-common 依赖项添加到您的构建中。