I am using an AsyncTask to send some messages to a server. This AsynTask is defined in a different class (CommTask.java) from the main activity class. The socket used, a websocket from weberknecht, is created in the doInBackground method of the AsyncTask, and I have a public method (SendDataToServer) in the CommTask.java, that is used in the main activity through an instance from the class CommTask.java.
I initialize the instance in the main activity before using the public method of CommTask:
commTask = new CommTask(serverIpAddress, socket, textStatus);
commTask.execute();
and when I have data to send, then I call:
commTask.SendDataToServer(...);
When it is used in the main activity the public method from CommTask.java and the socket uses http protocol
socket = new SocketIO("http://"+this.serverIpAddress+":3000/");
no exceptions are rised, but when it uses the https
socket = new SocketIO("https://"+this.serverIpAddress+":3000/");
android.os.NetworkOnMainThreadException apears.
Any idea? Why can this be happening?
If you need to take a look to the code see this:
https://github.com/Javi44/LocAALTOn/tree/WebSockets-Gottox/src/com/android/locaalton
EDIT:
Full error stack here:
03-12 15:14:36.090: W/System.err(27088): android.os.NetworkOnMainThreadException
03-12 15:14:36.100: W/System.err(27088): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
03-12 15:14:36.100: W/System.err(27088): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:686)
03-12 15:14:36.100: W/System.err(27088): at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
03-12 15:14:36.100: W/System.err(27088): at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
03-12 15:14:36.100: W/System.err(27088): at de.roderick.weberknecht.WebSocketConnection.send(WebSocketConnection.java:165)
03-12 15:14:36.100: W/System.err(27088): at io.socket.WebsocketTransport.send(WebsocketTransport.java:137)
03-12 15:14:36.100: W/System.err(27088): at io.socket.IOConnection.sendPlain(IOConnection.java:452)
03-12 15:14:36.100: W/System.err(27088): at io.socket.IOConnection.emit(IOConnection.java:825)
03-12 15:14:36.100: W/System.err(27088): at io.socket.SocketIO.emit(SocketIO.java:236)
03-12 15:14:36.100: W/System.err(27088): at com.android.locaalton.CommTask.SendDataToServer(CommTask.java:137)
03-12 15:14:36.105: W/System.err(27088): at com.android.locaalton.LocAALTOnActivity$2.onClick(LocAALTOnActivity.java:200)
03-12 15:14:36.105: W/System.err(27088): at android.view.View.performClick(View.java:4211)
03-12 15:14:36.105: W/System.err(27088): at android.view.View$PerformClick.run(View.java:17267)
03-12 15:14:36.105: W/System.err(27088): at android.os.Handler.handleCallback(Handler.java:615)
03-12 15:14:36.105: W/System.err(27088): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 15:14:36.105: W/System.err(27088): at android.os.Looper.loop(Looper.java:137)
03-12 15:14:36.105: W/System.err(27088): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-12 15:14:36.105: W/System.err(27088): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 15:14:36.105: W/System.err(27088): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 15:14:36.105: W/System.err(27088): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-12 15:14:36.105: W/System.err(27088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-12 15:14:36.105: W/System.err(27088): at dalvik.system.NativeStart.main(Native Method)