I developed the app That is working fine in before android 4.0.0 versions. But in android 4.0.0 + versions it getting force close. It say network on main thread exception
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The reason why your application crashes on
Android versions 3.0and above, but works fine onAndroid 2.xis because HoneyComb Ice Cream Sandwich and Jelly Bean are much stricter about abuse against the UI Thread. For example, when an Android device runningHoneyCombor above detects a network access on the UI thread, aNetworkOnMainThreadExceptionwill be thrown:The explanation as to why this occurs is well documented on the Android developer’s site:
A
NetworkOnMainThreadExceptionis 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.Some examples of other operations that
JellyBean,ICSandHoneyComband “ won’t allow you to perform on the UI thread are:If you are attempting to perform any of these operations on the UI thread, you must wrap them in a worker thread. The easiest way to do this is to use of an
AsyncTask, which allows you to perform asynchronous work on your user interface. An AsyncTask will perform the blocking operations in a worker thread and will publish the results on the UI thread, without requiring you to handle threads and/or handlers yourself.