Saturday, November 17, 2012

android.os.NetworkOnMainThreadException



android.os.NetworkOnMainThreadException exception in Android code??

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

If your code works in Android 2.x but throws this exception on Android 3.0 and above is because HoneyComb, Ice Cream Sandwich, JellyBean are much stricter about abuse against the UI Thread.. To prevent this exception you need to run the same code in ASyncTask..


The explanation as to why this exception occurs is well documented on the Android developer's site:

A NetworkOnMainThreadException is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

No comments:

Post a Comment