2017-08-15 149 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

相关问题