2

I using Java nio sockets in non blocking mode, but when i call write i still get NetworkOnMainThreadException.

java.nio.channels.SocketChannel fd = Network.createTcpSocket();
java.nio.channels.SocketChannel.open();
java.net.Socket socket = fd.socket();
socket.setTcpNoDelay(true);
socket.setKeepAlive(true);
fd.configureBlocking(false);

//.. conect the socket

int ret = fd.write(data);
if(ret == 0)
{
  // It will block.
}

I will expect this to work as the socket will not block and just return 0 in case the call need to block. But i still receive the NetworkOnMainThreadException

12-07 18:50:25.704: W/dalvikvm(14931): threadid=1: thread exiting with uncaught exception (group=0x40c501f8)
12-07 18:50:25.834: E/AndroidRuntime(14931): FATAL EXCEPTION: main
12-07 18:50:25.834: E/AndroidRuntime(14931): android.os.NetworkOnMainThreadException
12-07 18:50:25.834: E/AndroidRuntime(14931):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:168)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at libcore.io.IoBridge.sendto(IoBridge.java:477)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at java.nio.SocketChannelImpl.writeImpl(SocketChannelImpl.java:369)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at java.nio.SocketChannelImpl.write(SocketChannelImpl.java:327
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
José
  • 3,041
  • 8
  • 37
  • 58
  • how can you expect to write data in a non-blocking fashion and still get the number of bytes written on the next line? – njzk2 Dec 14 '12 at 13:40
  • Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer. – José Dec 14 '12 at 13:49
  • when would it write the rest, then? – njzk2 Dec 14 '12 at 13:50
  • you must call it later, when the selector told you that the SocketOperation.Write has end – José Dec 14 '12 at 13:52
  • but on which thread does it occur ? apparently it is still running on the ui – njzk2 Dec 14 '12 at 13:53
  • nio socket is non blocking mode doesn't block that is a fact, i was just surprised that Android disallow to use non blocking call in the main thread. – José Dec 14 '12 at 13:55

3 Answers3

2

Android's justification for throwing a NetworkOnMainThreadException
https://developer.android.com/reference/android/os/NetworkOnMainThreadException

is detailed in the document "Designing for Responsiveness"
https://developer.android.com/training/articles/perf-anr

But Android makes the mistake of assuming all network IO can cause UI unresponsiveness.

Java non-blocking NIO (and Java NIO.2) channel methods by definition do not block and thus cannot cause an unresponsive UI.
https://developer.android.com/reference/java/nio/channels/SelectableChannel#bm

A selectable channel is either in blocking mode or in non-blocking mode. In blocking mode, every I/O operation invoked upon the channel will block until it completes. In non-blocking mode an I/O operation will never block ...

It is a shame Android does not recognize this and thus causes developers to add an unnecessary background layer of abstraction (AsyncTask be the most clumsy) between the UI and Java non-blocking NIO network operations.

beyeriii
  • 301
  • 3
  • 6
1

It's a normal behavior. I guess there's a check inside of Socket class, no matter is it blocking and non-blocking. If thread is UI - throw NetworkOnMainThreadException

Taras
  • 2,526
  • 3
  • 33
  • 63
1

android.os.NetworkOnMainThreadException :

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

use AsyncTask for making network related task from Ui Thread

or you are using API LEVEL 9 then just set StrictMode for Current Activity in onCreate() method before making network request :

   StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
   .detectNetwork() // or .detectAll() for all detectable problems
   .penaltyDialog()  //show a dialog
   //.permitNetwork() //permit Network access 
   .build());
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • yes i read that before, but as all this was to avoid non blocking the UI thread, i was surprised that you canont do async io – José Dec 14 '12 at 13:57