NetworkOnMainThreadException on Android

180 views
Skip to first unread message

Bassam

unread,
Oct 13, 2014, 10:45:12 AM10/13/14
to dispatc...@googlegroups.com
Hi,

I get a NetworkOnMainThreadException when executing a simple Http request on Android 4.4.2 using scala 2.10.4 and android-sdk-plugin 0.13.5

The code:
(Http(host('some url') OK as.String) map { ... }

The error:
E/Upload  ( 7548): java.util.concurrent.ExecutionException: android.os.NetworkOnMainThreadException
E
/Upload  ( 7548):     at com.ning.http.client.providers.netty.NettyResponseFuture.abort(NettyResponseFuture.java:342)
E
/Upload  ( 7548):     at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.abort(NettyAsyncHttpProvider.java:1418)
E
/Upload  ( 7548):     at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.doConnect(NettyAsyncHttpProvider.java:1106)
E
/Upload  ( 7548):     at com.ning.http.client.providers.netty.NettyAsyncHttpProvider.execute(NettyAsyncHttpProvider.java:935)
E
/Upload  ( 7548):     at com.ning.http.client.AsyncHttpClient.executeRequest(AsyncHttpClient.java:499)
E
/Upload  ( 7548):     at dispatch.HttpExecutor$class.apply(execution.scala:47)
E
/Upload  ( 7548):     at dispatch.Http.apply(execution.scala:12)
E
/Upload  ( 7548):     at dispatch.HttpExecutor$class.apply(execution.scala:42)
E
/Upload  ( 7548):     at dispatch.Http.apply(execution.scala:12)
...

This topic mentions this issue but does not provide a solution: https://groups.google.com/d/topic/dispatch-scala/pJC5fwec-pA/discussion
(the initialization code suggested targeted another error and did not fix the issue)

If I wrap the request in a Future and flatten it afterwards, it does work:

Future { Http(host('some url') OK as.String) } flatMap (identity) map { ... }

But this seems a bit convoluted. Is it the expected behaviour, or am I missing something?

Thank you!

Nathan Hamblen

unread,
Oct 18, 2014, 8:25:20 PM10/18/14
to dispatc...@googlegroups.com
Well that is interesting. If you into the stack trace AHC does seem to
be doing some i/o on the calling thread:
https://github.com/AsyncHttpClient/async-http-client/blob/async-http-client-1.8.10/src/main/java/com/ning/http/client/providers/netty/NettyAsyncHttpProvider.java#L935

I'm sure there's a reason for it. And come to think of it, I have always
noticed a blocking pause on the first request when using dispatch in the
repl. The source code for the provider looks quite a bit different in
AHC 1.9 (still in beta), so there's some chance the behavior goes away
when we can upgrade to that.

For now I think backgrounding your request is the right thing to do.
Sorry it's awkward. Most people are interacting with disptach from a
thread where i/o is not completely forbidden. :)

If you're doing more than a little http interaction, you can make it
pretty without too much trouble.

object MyHttp {
implicit executor = MyExecutionContext
/** mimic apply methods of
https://github.com/dispatch/reboot/blob/0.11.3/core/src/main/scala/execution.scala#L36-L44
*/
def apply(req: Req): Future[Response] =
Future(dispatch.Http(req)).flatMap(identity)
[...]
}


On 10/13/2014 10:45 AM, Bassam wrote:
> Hi,
>
> I get a NetworkOnMainThreadException when executing a simple Http
> request on Android 4.4.2 using scala 2.10.4 and android-sdk-plugin 0.13.5
>
> The code:
> |
> |
> (Http(host('some url')OK as.String)map {...}
> |
>
> |The error:
> |
> E/Upload(7548):java.util.concurrent.ExecutionException:android.os.NetworkOnMainThreadException
> E/Upload(7548): at
> com.ning.http.client.providers.netty.NettyResponseFuture.abort(NettyResponseFuture.java:342)
> E/Upload(7548): at
> com.ning.http.client.providers.netty.NettyAsyncHttpProvider.abort(NettyAsyncHttpProvider.java:1418)
> E/Upload(7548): at
> com.ning.http.client.providers.netty.NettyAsyncHttpProvider.doConnect(NettyAsyncHttpProvider.java:1106)
> E/Upload(7548): at
> com.ning.http.client.providers.netty.NettyAsyncHttpProvider.execute(NettyAsyncHttpProvider.java:935)
> E/Upload(7548): at
> com.ning.http.client.AsyncHttpClient.executeRequest(AsyncHttpClient.java:499)
> E/Upload(7548): at dispatch.HttpExecutor$class.apply(execution.scala:47)
> E/Upload(7548): at dispatch.Http.apply(execution.scala:12)
> E/Upload(7548): at dispatch.HttpExecutor$class.apply(execution.scala:42)
> E/Upload(7548): at dispatch.Http.apply(execution.scala:12)
> ...
> |
>
> This topic mentions this issue but does not provide a
> solution:https://groups.google.com/d/topic/dispatch-scala/pJC5fwec-pA/discussion
> <https://groups.google.com/d/topic/dispatch-scala/pJC5fwec-pA/discussion>
> (the initialization code suggested targeted another error and did not
> fix the issue)
>
> If I wrap the request in a Future and flatten it afterwards, it does work:
>
> |
> Future{Http(host('some url')OK as.String)}flatMap (identity)map {...}
> |
>
> But this seems a bit convoluted. Is it the expected behaviour, or am I
> missing something?
>
> Thank you!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Dispatch" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to dispatch-scal...@googlegroups.com
> <mailto:dispatch-scal...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages