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);
         }
     };

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值