Android - NetworkOnMainThreadException using SpiderMonkey

I’m working on a multiplayer android game using SpiderMonkey.
However,network crashes and console returns this error:

android.os.NetworkOnMainThreadException

Lurking on the internet i found some docs talking about the AsyncTask class on Android’s API. It has to be used as Android>3.0 doesn’t support networking on main thread.

However i don’t know how to implement this in a JME game using SpiderMonkey. I’m pretty sure there’s a way to solve this issue but i can’t find it.

Before everyone can ask,yes,i added those lines to manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

That’s weird, because I am pretty sure SpiderMonkey starts a separate thread for networking. Can you post the full stack trace maybe?

From the client, I think the initial connection happens on the thread you open the connection. The message processing happens on a separate thread but connect() tries to do the actual socket hookup so that you get host not found errors, etc. right away. (That’s my recollection.)

So should i use AsyncTask when i create the client?

EDIT: I have an issue implementing this.

I have to create an AsyncTask on my SimpleApplication class,but how can i? Is there a way to import android’s api to the game project too?

Could anyone answer please? My game is pratically finished and i just need to port it to Android,but that’s the only thing that’s blocking me.

AsyncTask is only used for things that need the UI thread from what I understand.
You should be able to use standard Java threading classes to do this.
Start a new thread to create the connection then wait for it to exit. Or use an Executor and then shut it down when done.

Using an executor worked :smiley: Thanks alot!