Eclipseでokhttpライブラリ(jarファイル)を使用したい jarファイルをプロジェクトに追加して簡単なコードを書くが、私のapp.pleaseを実行するとエラーになるEclipseでokhttpを使用する方法
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();
}
}
エラー:アンドロイドアプリの開発のためのAndroid Studioに
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)
原因:java.lang.NoClassDefFoundError'。ライブラリを正しくコンパイルしていません –