httpclient.execute长时间停滞问题

最近在做一个android上的项目时,这个项目上有一项程序检测升级的功能,其中使用了apache的httpclient,在使用httpclient时,httpclient.execute(httpget)这个方法返回一个response对象,但是由于一些原因比如找不见httpget中url。或者url错误,导致httpclient.execute这个方法就会一直等待不会继续执行下面的程序,为了避免程序不卡在httpclient.execute这,我们把httpclient.execute放在一个线程中,让程序继续往下走,可以通过httpclient.execute返回对象是否为空来判断是否连接服务器的地址。

实例代码:

…………

           Thread hth = new Thread()

           {

 

              @Override

              public void run() {

                 

                 

                     try {

                    

                         response = client.execute(get);

                     } catch (IllegalArgumentException ec) {

                         response = null;

                         interrupted();

                     } catch (ClientProtocolException e) {

                        

                         response = null;

                         interrupted();

                     } catch (IOException e) {

                        

                         response = null;

                         interrupted();

                     }

                  }

             

             

           };

           hth.start();

 

           try {

          

              Thread.sleep(5000);

              if(response==null)

              {

                 

                  hth.interrupt();

             

                  return false;

              }

           } catch (InterruptedException e) {

             

              e.printStackTrace();

           }

          

          

          

          

          

          if(response.getStatusLine().getStatusCode()==200)

           {

              returngetDom(response.getEntity().getContent());

           }

……………………

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值