Question

My app is made to communicate with PLC using socket. I create a Socket for each PLC in the onCreate() and connect them.

Then I start two threads (because I got two types of PLC) and start communicating. The app works fine, I can communicate.

The point is I want to stop communication whilst my app is not in foreground.

@Override
 public void onStop()
 {
     super.onStop();

     thread_imo.Pause();
     thread_modbus.Pause();

     for(cPeriphIMO autom : automates)
     {
         if(autom.isConnected())
             autom.Deconnexion();
     }
 }

autom got the Socket and autom.Deconnexion just close this Socket.

But when it comes to restart the communication I got a problem.

@Override
 public void onRestart()
 {
     super.onRestart();

     for(cPeriphIMO autom : automates)
     {
         autom.Connexion();
     }

     thread_imo.Resume();
     thread_modbus.Resume();
 }

It's supposed to reconnect sockets, and I thought it would be easy.

public boolean Connexion()
{   
    try 
    {
        SocketAddress adr = new InetSocketAddress(ip, Integer.parseInt(port));
                    //THIS LINE CAUSES ERROR
        socket.connect(adr, 1000);
    } 
    catch(Exception e) {socket = null; Log.i("test", "error");}

    if (socket != null)
    {
        try 
        {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 
        } catch (Exception e) {return false;}

        return true;
    }
    else {return false;}        
}

But when I try to connect my socket, the app crashes, it doesn't even catch the error and I can't figure it out. Even though the socket is not null, i get NullPointerException :

07-08 15:51:46.330: E/AndroidRuntime(29498): FATAL EXCEPTION: main
07-08 15:51:46.330: E/AndroidRuntime(29498): java.lang.RuntimeException: Unable to resume activity {com.technicachat.webdatadomo/com.technicachat.webdatadomo.CIHM}: android.os.NetworkOnMainThreadException
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.os.Looper.loop(Looper.java:137)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at dalvik.system.NativeStart.main(Native Method)
07-08 15:51:46.330: E/AndroidRuntime(29498): Caused by: android.os.NetworkOnMainThreadException
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at libcore.io.IoBridge.connect(IoBridge.java:112)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at java.net.Socket.connect(Socket.java:842)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at com.technicachat.webdatadomo.IMO.CIMO.Connexion(CIMO.java:52)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at com.technicachat.webdatadomo.variables.cPeriphIMO.Connexion(cPeriphIMO.java:40)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at com.technicachat.webdatadomo.CIHM.onRestart(CIHM.java:591)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.Instrumentation.callActivityOnRestart(Instrumentation.java:1177)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.Activity.performRestart(Activity.java:5261)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.Activity.performResume(Activity.java:5272)
07-08 15:51:46.330: E/AndroidRuntime(29498):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
07-08 15:51:46.330: E/AndroidRuntime(29498):    ... 10 more

I tried to debug it myself, read tons of fix on this site, but I just can't seem to understand what is going on.

Thanks guys

Was it helpful?

Solution

It throws

android.os.NetworkOnMainThreadException

The reason for this error is that android doesn't like networking on the UI thread. Running such code on the UI thread will cause the app to hang. So Android forces you to run it in a background thread

Call the following code in an async task to make it work.

 for(cPeriphIMO autom : automates)
 {
     autom.Connexion();
 }

 thread_imo.Resume();
 thread_modbus.Resume();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top