-1

I am modifying an Android application. When I am running the app, all the functionalities work fine. But, in the backend, I am getting this error:

4008-4008/com.it.testApp W/System.err: android.os.NetworkOnMainThreadException

the line for the error is chatServise.java:

BaseActivity.xmppConnection.disconnect();
BaseActivity.xmppConnection = null;

I am using xmpp and smack for chat services.

The application is using this piece of code in other classes and no problems there. When I asked the development team, they told me that it is related to project setup. The application is working fine in every other aspects. I don't know how to solve this issue. Can anyone help?

My code is working perfectly in my previous developers system. But he won't help me. He only said 'you have to setup on server nodejs and need to do connection' I don't understand how to connect to node.js from android studio, I had spend 3 days on this and i couldn't find a solution. Can anyone help

I am adding full stack trace

05-25 11:51:46.433 3845-3845/com.it.example D/SMACK: 11:51:46 AM Connection closed (301630643) 05-25 11:51:46.433 3845-1453/com.it.example D/SMACK: 11:51:46 AM SENT (301630643): 05-25 11:51:46.584 3845-3845/com.it.example W/System.err: android.os.NetworkOnMainThreadException 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1090) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1085) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at org.jivesoftware.smack.XMPPConnection.shutdown(XMPPConnection.java:456) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at org.jivesoftware.smack.XMPPConnection.disconnect(XMPPConnection.java:482) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at org.jivesoftware.smack.Connection.disconnect(Connection.java:490) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at com.it.example.util.ChatService.onStart(ChatService.java:49) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.app.Service.onStartCommand(Service.java:458) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2904) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.app.ActivityThread.access$2100(ActivityThread.java:144) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.os.Looper.loop(Looper.java:135) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5311) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at java.lang.reflect.Method.invoke(Native Method) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at java.lang.reflect.Method.invoke(Method.java:372) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 05-25 11:51:46.594 3845-3845/com.it.example W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

shamna
  • 111
  • 1
  • 6

1 Answers1

1

I think the error is self explanatory you are running a network operation in the main thread.
You should allow all threads or find a more recent and efficient way to run the threads.
Use this code when you create the activity:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);


Look at this post. This question should be marked as duplicate actually.

Community
  • 1
  • 1
Manny265
  • 1,709
  • 4
  • 23
  • 42