1

I'm interested in main thread in a service (remote service) in android. I implemented service with 2 threads (one for reading from socket, one for sending data to server from the same socket). When I want to start connection (I do it only from the interior of "reading" thread and after it, if it pass, set special flags to inform "sending" thread that connection is set) I get error like this:

02-19 18:12:46.318: E/AndroidRuntime(4945): FATAL EXCEPTION: main
02-19 18:12:46.318: E/AndroidRuntime(4945): android.os.NetworkOnMainThreadException
02-19 18:12:46.318: E/AndroidRuntime(4945):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.io.InputStreamReader.read(InputStreamReader.java:244)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.io.BufferedReader.readLine(BufferedReader.java:354)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at com.example.aaa.MyService.try_to_connect_with_server(MyService.java:640)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at com.example.aaa.MyService.URUCHOM_SIEC(MyService.java:510)
02-19 18:12:46.318: E/AndroidRuntime(4945):    at com.example.aaa.MyService$Handler_X.handleMessage(MyService.java:219)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at android.os.Looper.loop(Looper.java:137)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at java.lang.reflect.Method.invoke(Method.java:511)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-19 18:12:46.318: E/AndroidRuntime(4945):     at dalvik.system.NativeStart.main(Native Method)

in code like this below (in row with in.readline):

            ...
            socket.connect(new InetSocketAddress(ADRES, 6000), 1200);
            if (socket != null) {
                in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                out = new BufferedWriter(new OutputStreamWriter(
                        socket.getOutputStream()));
                out.write("login_message\n");
                out.flush();
                response = in.readLine(); //<<<===== errors occur here
            ...


                          //setting flags about established connection
                }

I don't understand what is a main thread (in this situation) and why only sometimes these errors occur? I apologize if my ask is elementary, but I couldn't find understanding (by me) answer for it. I add simple and bad looking flow control chart (I'm amateur): enter image description here

Regards, Artik

Artik
  • 803
  • 14
  • 36
  • Rather not:(. I use inner thread inside service what is one of given resolves in suggested post. – Artik Feb 19 '13 at 19:28

2 Answers2

2

I don't understand what is a main thread (in this situation)

Each process has what is referred to as the main application thread. In processes with UI (activities), the main application thread drives the UI. It is also the thread used for lifecycle methods (e.g., onStartCommand() of a service).

In your case, you are on the main application thread because you used a Handler, in a service, named com.example.aaa.MyService. Having a Handler in a Service is bizarre, IMHO.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you. This handler is for reading message from activity which use MyService. It is strange for me, it can perform error because I can't find direct influence this handler on connection in my code. I have to dig deeper to find this corelation with hadler. Thank you again. – Artik Feb 19 '13 at 18:49
  • @Artik: "This handler is for reading message from activity which use MyService" -- I do not recommend this at all. Please use the command pattern (`startService()` and `onStartCommand()`) or the binding pattern (`bindService()` and `onBind()` with a `Binder`). – CommonsWare Feb 19 '13 at 18:52
  • Yes, I use it: I return mMessenger.getBinder() in onBind where mMessenger is: final Messenger mMessenger = new Messenger(new Handler_X()); where Handler X is this problematic Handler responsible for errors in my code. Is it correct? I apologize for my english (source of misunderstanding). – Artik Feb 19 '13 at 19:24
  • @Artik: "is this problematic Handler responsible for errors in my code. Is it correct?" -- yes, because `handleMessage()` will be invoked on the main application thread, and you cannot do long operations like network I/O on the main application thread, even of a remote service. – CommonsWare Feb 19 '13 at 19:38
  • Yes. You have right. I found my fault: I had invoking connection twice (one from realy main thread). Thank you. – Artik Feb 19 '13 at 19:55
0

You cannot open a network connection on the Main UI Thread. You need to move to a separate thread.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • Thank you. Yes, I have separate thread and I think here isn't problem UI thread - it is inside remote service - without UI (as I well understand?). – Artik Feb 19 '13 at 18:44
  • Explain the flow of how your service works by a FlowDiagram that way we can see where the problem is. – JoxTraex Feb 19 '13 at 18:46