Reputation: 1
i want to use okhttp library(jar file) in eclipse i add jar file to my project and write a simple code,but i get following error when run my app.please help me ...
public void clicked(View v) throws IOException{
TestMain example = new TestMain();
example.doGetRequest("http://www.vogella.com");
}
public class TestMain {
OkHttpClient client = new OkHttpClient();
// code request code here
String doGetRequest(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
}
error :
01-15 12:55:58.561: E/dalvikvm(2133): Could not find class 'com.squareup.okhttp.OkHttpClient', referenced from method com.example.http1.MainActivity$TestMain.<init>
01-15 12:55:58.571: E/dalvikvm(2133): Could not find class 'com.squareup.okhttp.Request$Builder', referenced from method com.example.http1.MainActivity$TestMain.doGetRequest
01-15 12:55:58.981: E/AndroidRuntime(2133): FATAL EXCEPTION: main
01-15 12:55:58.981: E/AndroidRuntime(2133): Process: com.example.http1, PID: 2133
01-15 12:55:58.981: E/AndroidRuntime(2133): java.lang.IllegalStateException: Could not execute method of the activity
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.view.View$1.onClick(View.java:3823)
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.view.View.performClick(View.java:4438)
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.view.View$PerformClick.run(View.java:18422)
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.os.Handler.handleCallback(Handler.java:733)
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.os.Handler.dispatchMessage(Handler.java:95)
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.os.Looper.loop(Looper.java:136)
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-15 12:55:58.981: E/AndroidRuntime(2133): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 12:55:58.981: E/AndroidRuntime(2133): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 12:55:58.981: E/AndroidRuntime(2133): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-15 12:55:58.981: E/AndroidRuntime(2133): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-15 12:55:58.981: E/AndroidRuntime(2133): at dalvik.system.NativeStart.main(Native Method)
01-15 12:55:58.981: E/AndroidRuntime(2133): Caused by: java.lang.reflect.InvocationTargetException
01-15 12:55:58.981: E/AndroidRuntime(2133): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 12:55:58.981: E/AndroidRuntime(2133): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 12:55:58.981: E/AndroidRuntime(2133): at android.view.View$1.onClick(View.java:3818)
01-15 12:55:58.981: E/AndroidRuntime(2133): ... 11 more
01-15 12:55:58.981: E/AndroidRuntime(2133): Caused by: java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient
01-15 12:55:58.981: E/AndroidRuntime(2133): at com.example.http1.MainActivity$TestMain.<init>(MainActivity.java:36)
01-15 12:55:58.981: E/AndroidRuntime(2133): at com.example.http1.MainActivity.clicked(MainActivity.java:26)
01-15 12:55:58.981: E/AndroidRuntime(2133): ... 14 more
01-15 13:00:33.731: E/dalvikvm(2302): Could not find class 'com.squareup.okhttp.OkHttpClient', referenced from method com.example.http1.MainActivity$TestMain.<init>
01-15 13:00:33.801: E/AndroidRuntime(2302): FATAL EXCEPTION: main
01-15 13:00:33.801: E/AndroidRuntime(2302): Process: com.example.http1, PID: 2302
01-15 13:00:33.801: E/AndroidRuntime(2302): java.lang.IllegalStateException: Could not execute method of the activity
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.view.View$1.onClick(View.java:3823)
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.view.View.performClick(View.java:4438)
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.view.View$PerformClick.run(View.java:18422)
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.os.Handler.handleCallback(Handler.java:733)
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.os.Handler.dispatchMessage(Handler.java:95)
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.os.Looper.loop(Looper.java:136)
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-15 13:00:33.801: E/AndroidRuntime(2302): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:00:33.801: E/AndroidRuntime(2302): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 13:00:33.801: E/AndroidRuntime(2302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-15 13:00:33.801: E/AndroidRuntime(2302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-15 13:00:33.801: E/AndroidRuntime(2302): at dalvik.system.NativeStart.main(Native Method)
01-15 13:00:33.801: E/AndroidRuntime(2302): Caused by: java.lang.reflect.InvocationTargetException
01-15 13:00:33.801: E/AndroidRuntime(2302): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:00:33.801: E/AndroidRuntime(2302): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 13:00:33.801: E/AndroidRuntime(2302): at android.view.View$1.onClick(View.java:3818)
01-15 13:00:33.801: E/AndroidRuntime(2302): ... 11 more
01-15 13:00:33.801: E/AndroidRuntime(2302): Caused by: java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient
01-15 13:00:33.801: E/AndroidRuntime(2302): at com.example.http1.MainActivity$TestMain.<init>(MainActivity.java:37)
01-15 13:00:33.801: E/AndroidRuntime(2302): at com.example.http1.MainActivity.clicked(MainActivity.java:27)
01-15 13:00:33.801: E/AndroidRuntime(2302): ... 14 more
01-15 13:02:09.921: E/dalvikvm(2401): Could not find class 'com.squareup.okhttp.OkHttpClient', referenced from method com.example.http1.MainActivity$TestMain.<init>
01-15 13:02:10.131: E/AndroidRuntime(2401): FATAL EXCEPTION: main
01-15 13:02:10.131: E/AndroidRuntime(2401): Process: com.example.http1, PID: 2401
01-15 13:02:10.131: E/AndroidRuntime(2401): java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient
01-15 13:02:10.131: E/AndroidRuntime(2401): at com.example.http1.MainActivity$TestMain.<init>(MainActivity.java:37)
01-15 13:02:10.131: E/AndroidRuntime(2401): at com.example.http1.MainActivity.onCreate(MainActivity.java:21)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.app.Activity.performCreate(Activity.java:5231)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.os.Handler.dispatchMessage(Handler.java:102)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.os.Looper.loop(Looper.java:136)
01-15 13:02:10.131: E/AndroidRuntime(2401): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-15 13:02:10.131: E/AndroidRuntime(2401): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:02:10.131: E/AndroidRuntime(2401): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 13:02:10.131: E/AndroidRuntime(2401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-15 13:02:10.131: E/AndroidRuntime(2401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-15 13:02:10.131: E/AndroidRuntime(2401): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 2274
Reputation: 643
Switch to Android Studio for Android App Development.
Eclipse is no longer supported. With Android Studio, you can add the following line to your build.gradle file to easily add OkHttp without having to manually deal with JARs.
compile 'com.squareup.okhttp3:okhttp:3.5.0'
Upvotes: 1