2017-01-15 1 views
0

je veux utiliser la bibliothèque okhttp (fichier jar) en eclipse je ajoute le fichier jar à mon projet et écrire un code simple, mais je reçois erreur suivante lors de l'exécution de mon app.please m'aider ...comment utiliser okhttp en éclipse

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(); 
    } 

}

erreur:

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) 
+0

'Causée par: java.lang.NoClassDefFoundError'. Vous avez incorrectement compilé la bibliothèque –

Répondre

1

Passer à Android studio App Android développement.

Eclipse n'est plus supporté. Avec Android Studio, vous pouvez ajouter la ligne suivante à votre fichier build.gradle pour ajouter facilement OkHttp sans avoir à traiter manuellement les fichiers JAR.

compile 'com.squareup.okhttp3:okhttp:3.5.0' 
+0

Eclipse prend en charge Gradle maintenant, au fait –

+0

OK, mais ADT n'est plus supporté sur Eclipse. Il est fortement recommandé que OP passe à Android Studio, car cela résoudra ce problème. https://android-developers.googleblog.com/2015/06/an-update-on-eclipse-android-developer.html – Faraz

+0

Parce que Google ne supporte pas cela ne signifie pas qu'il ne peut pas être utilisé. C'est mon point. De plus, l'ajout d'OkHttp ne devrait pas importer si c'est un projet Android ou non –