Android3.0版本以上出现android.os.NetworkOnMainThreadException异常与处理方式

 

以前用2.2 访问WebService没有问题,到3.0上访问出现android.os.NetworkOnMainThreadException

找了资料经过实践,解决方法如下:

 

?
StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork() // or
                                                                        // .detectAll()
                                                                        // for
                                                                        // all
                                                                        // detectable
                                                                        // problems
                .penaltyLog().build());
        StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());

 

PS:1.在2.3版本以后加入了StrictMode类,而在3.0在网络上能感觉到有更加严格的限制,更多的查询API上的StrictMode ;       2.使用的时候只需要在你项目运行的入口Activity的OnCreate中放入这段代码,那么整个项目程序都有用。不需要每个Activity里面加入。

      3.StrictMode类一般是用来调试的,在程序运行中会打印很多消息,那是告诉你你的项目程序需要改进的地方。在Android项目中,最好的是让界面与后台装载程序分开来。总之,如果你的程序代码非常符合Android规范要求,那么你完全可以不使用上面的代码...

 

别一种方法handler中更新UI。

    TextView text1, text2;
     Button button;
     Thread th;
 
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         text1 = (TextView)findViewById(R.id.text1);
         text2 = (TextView)findViewById(R.id.text2);
         button = (Button)findViewById(R.id.button1);
         button.setOnClickListener( new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 // TODO Auto-generated method stub
                 
                 th = new Thread(runnable);
                 th.start();
             }
         });
     }
  Handler myHandler = new Handler(){
         @Override
         public void handleMessage(Message msg) {
             // TODO Auto-generated method stub
             switch (msg.what) {
             case 1 :
                 System.out.println(msg.getData().getString("s1"));
                 System.out.println(msg.getData().getString( "s2" ));
                 try {
                  text1.setText(msg.getData().getString( "s1" ));
                  text2.setText(msg.getData().getString( "s1" ));
                 } catch (Exception e){
                     e.printStackTrace();
                 }
                 break ;
 
             default :
                 break ;
             }
             super .handleMessage(msg);
         }
     };
 
     Runnable runnable = new Runnable() {
         @Override
         public void run() {
               String s1 = "fsdfsgfdsgdfgfdgdhshshs" ;
               String s2 = "fsfsdgdshdhdshrehreherh" ;
 
               Message msg = new Message();
               msg.what = 1 ;
               Bundle bundle = new Bundle();
               bundle.putString( "s1" , s1);
               bundle.putString( "s2" , s2);
               msg.setData(bundle);
               MainActivity. this .myHandler.sendMessage(msg);
         }
     };

 

 

你可能感兴趣的:(exception)