Look at the name should know, is the network request in Mainthread the exception that produces
http://blog.csdn.net/mad1989/article/details/25964495
Look at the name should know, is the network request in Mainthread the exception that produces
First look at the explanation of the official website:
Class Overview
The exception is thrown when an application attempts to perform a networking operation on its main thread.
This is a thrown for applications targeting the honeycomb SDK or higher. Applications targeting earlier SDK versions is allowed to does networking on their main event loop threads, but it's heavil Y discouraged. See the document designing for responsiveness.
Also See StrictMode
.
Http://developer.android.com/intl/zh-cn/reference/android/os/NetworkOnMainThreadException.html
To explain, from the Honeycomb SDK (3.0), Google no longer allows the network request (HTTP, Socket) and other related operations directly in the main thread class, in fact, should not do so, directly in the UI thread network operation, will block the UI, The user experience is quite bad! Even if Google doesn't forbid it, we wouldn't normally do it.
So, in other words, in the Honeycomb SDK (3.0) The following version, you can continue to do so in the main thread, at more than 3.0, it is not, it is recommended
1, and the network for more time-consuming operations into a sub-thread, and then use the handler message mechanism to communicate with the main thread.
[Java]View Plaincopy
- Public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- This.setcontentview (r.layout.test);
- //Open a sub-thread for network operation, wait for return result, use handler to notify UI
- new Thread (Networktask). Start ();
- }
- Handler Handler = new Handler () {
- @Override
- public void Handlemessage (Message msg) {
- super.handlemessage (msg);
- Bundle data = Msg.getdata ();
- String val = data.getstring ("value");
- LOG.I ("MyLog", "request Result--" + val);
- //TODO
- //UI update and other related actions
- }
- };
- /**
- * Network operation related sub-threads
- */
- Runnable networktask = new Runnable () {
- @Override
- public Void Run () {
- //TODO
- //HTTP request is made here. Network requests related Operations
- Message msg = new Message ();
- Bundle data = new bundle ();
- Data.putstring ("value", "request result");
- Msg.setdata (data);
- Handler.sendmessage (msg);
- }
- };
2, using asynchronous mechanisms such as: Asynctask, this is a simple example of loading a network image
[Java]View Plaincopy
- Class Downimage extends Asynctask {
- private ImageView ImageView;
- Public downimage (ImageView ImageView) {
- This.imageview = ImageView;
- }
- @Override
- protected Bitmap doinbackground (String ... params) {
- String url = params[0];
- Bitmap Bitmap = null;
- try {
- //Load a network picture
- InputStream is = new URL (URL). OpenStream ();
- Bitmap = Bitmapfactory.decodestream (IS);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return bitmap;
- }
- @Override
- protected void OnPostExecute (Bitmap result) {
- Imageview.setimagebitmap (result);
- }
- }
3, directly in the main Thread of the network operation method, given online, I did not specifically test:
Add the following code to the OnCreate function inside the activity that initiated the HTTP request:
[Java]View Plaincopy
- Strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder ()
- . Detectdiskreads (). Detectdiskwrites (). Detectnetwork ()
- . Penaltylog (). build ());
- Strictmode.setvmpolicy (new StrictMode.VmPolicy.Builder ()
- . Detectleakedsqlliteobjects (). Detectleakedclosableobjects ()
- . Penaltylog (). Penaltydeath (). build ());
Keep in mind that if you declare a handler in the main thread, the handler post Runnable (thread) and the message processed are in the current Mian thread, not a child thread.
Reproduced Android's networkonmainthreadexception anomaly