hidroh / materialistic

A material-design Hacker News Android reader
https://play.google.com/store/apps/details?id=io.github.hidroh.materialistic
Apache License 2.0
2.3k stars 508 forks source link

NetworkOnMainThreadException from sync adapter? #429

Closed hidroh closed 8 years ago

hidroh commented 8 years ago

Sync adapter runs on a separate process, but I guess it's still considered main thread?

Fatal Exception: android.os.NetworkOnMainThreadException
       at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1161)
       at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
       at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
       at java.net.InetAddress.getAllByName(InetAddress.java:215)
       at okhttp3.Dns$1.lookup(Dns.java:39)
       at okhttp3.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:173)
       at okhttp3.internal.http.RouteSelector.nextProxy(RouteSelector.java:139)
       at okhttp3.internal.http.RouteSelector.next(RouteSelector.java:81)
       at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
       at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
       at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
       at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
       at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
       at okhttp3.RealCall.getResponse(RealCall.java:240)
       at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
       at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:142)
       at io.github.hidroh.materialistic.ActivityModule$LoggingInterceptor.intercept(ActivityModule.java:264)
       at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
       at io.github.hidroh.materialistic.ActivityModule$ConnectionAwareInterceptor.intercept(ActivityModule.java:223)
       at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
       at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
       at okhttp3.RealCall.execute(RealCall.java:57)
       at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
       at io.github.hidroh.materialistic.data.ReadabilityClient$Impl.readabilityParse(ReadabilityClient.java:114)
       at io.github.hidroh.materialistic.data.ReadabilityClient$Impl.parse(ReadabilityClient.java:98)
       at io.github.hidroh.materialistic.data.ItemSyncAdapter.syncReadability(ItemSyncAdapter.java:216)
       at io.github.hidroh.materialistic.data.ItemSyncAdapter.access$200(ItemSyncAdapter.java:61)
       at io.github.hidroh.materialistic.data.ItemSyncAdapter$1.onResponse(ItemSyncAdapter.java:198)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5343)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
hidroh commented 8 years ago

Apparently Retrofit callbacks will be called on main thread, regardless on which thread the request has been executed:

On Android, callbacks will be executed on the main thread. On the JVM, callbacks will happen on the same thread that executed the HTTP request. http://square.github.io/retrofit/