If the "tiapp.xml" file property run-on-main-thread is set to true, then calling a Ti.Networking.Socket.TCP object's blocking read() or write() method will cause a crash on Android.
Steps to reproduce:
- Open the project's "tiapp.xml" file.
- Make sure property "run-on-main-thread" is set to true.
- Set up an Android device with Internet access.
- Build and run TcpClientBlockingTest.js on the Android device.
The app crashes with a Java NetworkOnMainThreadException.
The Java java.net.ServerSocket class will throw a NetworkOnMainThreadException when used on the main UI thread. It can only be used by a non-UI thread.
Simplest solution is to set the tiapp.xml property "run-on-main-thread" to false.
Best solution is to not use the blocking read() and write() methods and use the async Ti.Stream.pump() and Ti.Stream.write() methods instead as documented here...
Note that even if we do fix the crash, we still don't recommend using the synchronous read() and write() method calls since they'll block the UI thread. This will make the UI appear to stutter or hang, providing a poor experience for the end-user.