2017-08-15 188 views
0
使用改型2,下載並在我的代碼解析JSON文件到模型類 但我給了一堆關於在這裏異常

嗨IAM是我的錯誤相關的代碼:retrofit2例外:無法啓動活動ComponentInfo

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

這是我的接口

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

}

,這些都是異常堆棧:

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) 

感謝您的幫助

回答

1

您在MainThread做網絡電話撥打call.execute()改用call.enqueue(callback)它工作者線程執行網絡呼叫與還給通過對MainThread回調響應。

欲瞭解更多信息:https://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html

+0

感謝您的幫助,但我在回調了各種錯誤部分 –

+0

錯誤都不錯。必須與當前的不同,對嗎? – Omkar

相關問題