android中显示 android.os.NetworkOnMainThreadException

今天下午写了一下午的程序,怎么看都没有任何错误,服务器端运行正确,客户端一直有问题。

排查了各种可能的错误之后,我想到是不是应该用线程(一遇到线程头就大)。

由于是对listview中的某一个item中的按钮控件进行监听的事件,单独用线程是不可能实现的。

后来发现一个可行的办法,和大家分享一下。

在我的例子中

对于botton事件的监听

 

 button.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                         .detectDiskReads()
                         .detectDiskWrites()
                        .detectNetwork()  
                         .penaltyLog()
                         .build());
                      StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                         .detectLeakedSqlLiteObjects()
                         .detectLeakedClosableObjects()
                         .penaltyLog()
                         .penaltyDeath()
                         .build());      
                                     。。。。。(写自己的代码)         }  }

或者如果你的程序可以使用线程的话,也是可行的。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                 new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog,int whichButton) {
                                        JSONObject bill_json = null;
                                      String is_feename= name.getText().toString();
                                      String is_freightmemo=freightmemo.getText().toString();
                                      String  is_freightmemodownload= freightmemodownload.getText().toString();                   
                                      String is_fipid=t_fipid.getText().toString();
                                      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());                

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值