Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkOnMainThreadException #2017

Closed
juliocbcotta opened this issue Nov 20, 2015 · 7 comments
Closed

NetworkOnMainThreadException #2017

juliocbcotta opened this issue Nov 20, 2015 · 7 comments

Comments

@juliocbcotta
Copy link

Whats up doc?
I am getting this exception when running requests in wss with okhttp-ws..
If you want to test it, just run the code here https://github.com/BugsBunnyBR/Rocket.Chat.Android
I tried with 2.5.0 and with 2.6.0-SNAPSHOT, same problem.

android.os.NetworkOnMainThreadException
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:755)
okio.Okio$1.write(Okio.java:80)
okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
okio.RealBufferedSink.emit(RealBufferedSink.java:183)
com.squareup.okhttp.internal.ws.WebSocketWriter.writeMessageFrameSynchronized(WebSocketWriter.java:210)
com.squareup.okhttp.internal.ws.WebSocketWriter.access$500(WebSocketWriter.java:46)
com.squareup.okhttp.internal.ws.WebSocketWriter$FrameSink.close(WebSocketWriter.java:264)
okio.RealBufferedSink.close(RealBufferedSink.java:241)
com.squareup.okhttp.internal.ws.RealWebSocket.sendMessage(RealWebSocket.java:131)
chat.rocket.operations.meteor.Meteor.send(Meteor.java:369)
chat.rocket.operations.meteor.Meteor.send(Meteor.java:351)
chat.rocket.operations.meteor.Meteor.callWithSeed(Meteor.java:964)
chat.rocket.operations.meteor.Meteor.call(Meteor.java:912)
chat.rocket.operations.meteor.Meteor.login(Meteor.java:779)
chat.rocket.operations.meteor.Meteor.loginWithUsername(Meteor.java:739)
chat.rocket.operations.methods.RocketMethods.login(RocketMethods.java:128)
chat.rocket.app.ui.activities.HomeActivity.onConnect(HomeActivity.java:283)
chat.rocket.operations.meteor.MeteorSingleton.onConnect(MeteorSingleton.java:93)
chat.rocket.operations.meteor.Meteor$6.run(Meteor.java:1122)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:148)
android.app.ActivityThread.main(ActivityThread.java:5417)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

@JakeWharton
Copy link
Member

sendMessage is synchronous and must be done on a background thread.

@juliocbcotta
Copy link
Author

This is weird,
if I test with my local network server (my notebook) and ws, I don't get this error.
Maybe, the local network is so fast that android do not see this ?

@JakeWharton
Copy link
Member

Perhaps? All interactions with the WebSocket instance are synchronous to
the network, though, so they must be done off the main thread.

On Fri, Nov 20, 2015 at 12:58 AM Júlio Cesar Bueno Cotta <
notifications@github.com> wrote:

This is weird,
if I test with my local network server (my notebook) and ws, I don't get
this error.
Maybe, the local network is so fast that android do not see this ?


Reply to this email directly or view it on GitHub
#2017 (comment).

@juliocbcotta
Copy link
Author

Would you mind to add some documentation to the sendMessage behavior or maybe think about a different syntax like execute and enqueue to make is more explicit the synchronous behavior?
Just asking, but ... Would it be hard to make WebSocketListener and the WebSocket methods reactive?

@JakeWharton
Copy link
Member

The entire class defines itself as blocking
https://github.com/square/okhttp/blob/master/okhttp-ws/src/main/java/com/squareup/okhttp/ws/WebSocket.java#L23
and
the void methods throwing IOException should also be indicative of work
being done synchronously here.

You could make the listener and observable, sure. The WebSocket itself is
probably more of a function (i.e., map) from data to success (no exception)
or failure (exception).

On Fri, Nov 20, 2015 at 1:51 AM Júlio Cesar Bueno Cotta <
notifications@github.com> wrote:

Would you mind to add some documentation to the sendMessage behavior or
maybe think about a different syntax like execute and enqueue to make is
more explicit the synchronous behavior?
Just asking, but ... Would it be hard to make WebSocketListener and the
WebSocket methods reactive?


Reply to this email directly or view it on GitHub
#2017 (comment).

@swankjesse
Copy link
Member

No action to take here.

@juliocbcotta
Copy link
Author

Thanks for the help doc!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants