2
votes

This is the first time I work with OkHttp, and I want to study some basic codes first.

So this is the code, taken from here:

package com.anta40.app.okconnectiontest;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainClass {

    public static void main(String args[]) {
        OkHttpClient okcl = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://www.vogella.com/index.html")
                .build();

        okcl.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code " + response);
                } 
                else {
                    System.out.println("okay.....");
                }
            }
        });
    }

}

Running the code on Eclipse yields this output:

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/TypeCastException at okhttp3.ResponseBody.create(ResponseBody.java:210) at okhttp3.internal.Util.(Util.java:60) at okhttp3.OkHttpClient.(OkHttpClient.java:123) at com.anta40.app.okconnectiontest.MainClass.main(MainClass.java:14) 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:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 4 more

I have OkHttp and kotlin jars in my build path: enter image description here enter image description here enter image description here

I'm on Windows 10 64, with JDK 1.8.0_181, OkHttp 3.11, OkIo 2.1.0, kotlib-stdlib -0.6.179, kotlin-stdlib-common 1.2.71 What's wrong here?

1
Looks like a duplicate of stackoverflow.com/questions/51563273/… Also, that exception means kotlin/TypeCastException is missing from the classpath. Sometimes newer/older jar versions have different path or names for certain classes. So in your eclipse, in the package explorer window, open your project, click on "Referenced Libraries" and then open the kotlin jar and check whether kotlin/TypeCastException exists. If it doesn't, then you are using a wrong version of the jarAniket Paul

1 Answers

5
votes

The code worked for me with okhttp-3.9.0.jar & okio-1.13.0.jar.