I just found out about NetworkOnMainThreadException at official docs
and was wondering if the emulator is throwing this. I have been testing my app quite a bit and as far as I know all networking is off the main thread (using Roboguice RoboAsyncTask) but you never know if one has not escaped.
I am also using StrictMode and have not seen anything.
Is my code just clean or is this not thrown on the emulator?
How are we supposed to prepare for this happening in production?
What about a grace period or something? Or is that elapsed now ;-) ??
With honeycomb you can not perform a networking operation on its main thread as documentation says. For this reason you must use handler or asynctask. There is no another way to do it.
here you can find 2 examples written in turkish about networking operation. maybe they help.
3. party kütüphane kullanmadan (ksoap2), (it includes english translation)
AsyncTask class'tan dönen parametreyi handle etmek. , google translate
From Honeycomb SDK (3), Google will no longer allow network requests (HTTP, Socket) and other related operations directly in the Main Thread class, in fact, should not do direct network operation in the UI thread, blocking UI, user experience is bad! Even if Google is not prohibited, under normal circumstances, we will not do it ~! So, that is, in the Honeycomb SDK (3) version, you can also continue to do so in Main Thread, more than 3, it will not work.
1.use
Handler
The more time-consuming operations associated with network are placed into a child thread and then communicated with the main thread using the
Handler
messaging mechanism2.use
AsyncTask
3.use
StrictMode
If you are running on 3.0, i can't help; Because Strict mode is on by default in it; But its above tat, then this might help
Include this before your HTTP connection creation; Then it works
I have tested this and it does in fact happen on the emulator as well. Better make sure you test your app at least on the emulator if you plan to get it onto the 3.0 tablets and beyond.
NetworkOnMainThreadException
occurs when some networking operations are performed inside main method; I meanOncreate()
. You can useAsyncTask
for resolving this issue. or you can useinside
onCreate()
method.