2017-08-15 5 views
0

Salut iam utilisant retrofit 2 pour télécharger et analyser un fichier JSON à une classe de modèle dans mon code, mais je lui ai donné un tas d'exception à ce sujet ici est mon code lié à l'erreur:exception retrofit2: Impossible de démarrer l'activité ComposantInfo

Retrofit retrofit = new Retrofit.Builder().baseUrl("http://services.hanselandpetal.com").addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    getFlowers getFlowers = retrofit.create(getFlowers.class); 
    Call<Flowers> call = getFlowers.all(); 

     Response <Flowers> response = call.execute(); 

ceci est mon interface

public interface FlowerAPI { 
@GET("feeds/flowers.json") 
Call<List<Flower>> getAllFlowers(); 

}

et ceux-ci sont la pile d'exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.uefi.retet/com.example.uefi.retet.MainActivity}: android.os.NetworkOnMainThreadException 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
       Caused by: android.os.NetworkOnMainThreadException 
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) 
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86) 
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) 

apprécient votre aide

Répondre

1

Vous faites appel réseau sur MainThread en appelant call.execute() utiliser à la place call.enqueue(callback) il exécute appel réseau sur thread de travail et rend la réponse par le rappel en cas MainThread.

Pour plus d'informations: https://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html

+0

Merci pour votre aide, mais je suis arrivé sur diverses erreurs Callback section –

+0

erreurs sont bonnes. Doit être différent de celui en cours, non? – Omkar