2011-05-04 86 views
0

我有以下代码,我希望能够重新启动线程,如果在处理请求时发生异常。黑莓如果失败重试连接的最佳方式?

INT状态= httpConn.getResponseCode)(;:

的在一个线程的运行方法以下

     if (status == HttpConnection.HTTP_OK) { 
          // Is this html? 
          String contentType = httpConn 
            .getHeaderField(HEADER_CONTENTTYPE); 
          boolean htmlContent = (contentType != null && contentType 
            .startsWith(CONTENTTYPE_TEXTHTML)); 

          InputStream input = s.openInputStream(); 

          byte[] data = new byte[1000]; 
          int len = 0; 
          int size = 0; 
          StringBuffer raw = new StringBuffer(); 

          while (-1 != (len = input.read(data))) { 
           // Exit condition for the thread. An 
           // IOException 
           // is 
           // thrown because of the call to 
           // httpConn.close(), 
           // causing the thread to terminate. 
           if (_stop) { 
            httpConn.close(); 
            s.close(); 
            input.close(); 
           } 
           raw.append(new String(data, 0, len)); 
           size += len; 
          } 

          // raw.insert(0, "bytes received]\n"); 
          // raw.insert(0, size); 
          // raw.insert(0, '['); 
          content = raw.toString(); 

          if (htmlContent) { 
           content = prepareData(raw.toString()); 
          } 
          input.close(); 
         } else { 
          try{ 
           httpConn.close(); 
          }catch (Exception e) { 
           // TODO: handle exception 
          } 
          errorDialog(status+", status code"); 

          retryFeed(getUrl(), "Network error. Retrying..."); 

         } 
         s.close(); 
        } else { 



         errorDialog("Sorry Insufficient Network Coverage."); 
         return; 
        } 
       } catch (IOCancelledException e) { 

         errorDialog(e.getMessage()); 
        retryFeed(getUrl(), "Network error. Retrying..."); 
       } catch (IOException e) { 

        errorDialog(e.getMessage()); 
        retryFeed(getUrl(), "Network error. Retrying..."); 
       } 

如果失败重试连接最安全的方法是什么?

谢谢。

//新建这是错误主题。检查连接中的错误......这会有帮助吗?它是最有效的方法吗?感谢..

/错误线程 - 线程来检查错误/ 私有类ErrorThread继承Thread { 私有静态最终诠释TIMEOUT = 3000; //每3秒 private boolean hasException = false; private String _theUrl;

/** 
    * Stops this thread from listening for messages 
    */ 
    private synchronized void stop() 
    { 
     hasException =false; 
    } 

    /** 
    * Listens for incoming messages until stop() is called 
    * @see #stop() 
    * @see java.lang.Runnable#run() 
    */ 
    public void run() 
    { 
     try 
     {  

       while (true) { 



        if((hasException==true)) 
        { 
         // Synchronize here so that we don't end up creating a connection that is never closed. 
        errorDialog("Will Fetch new"); 
         synchronized(this) 
         { 
          hasException=false; 
          if (!_connectionThread.isStarted()) { 


          fetchPage(_theUrl); 
         } else { 

          createNewFetch(_theUrl); 

         } 

         } 

       } 
        try { 

         //errorDialog("No exception."); 

         sleep(TIMEOUT); 

        } catch (InterruptedException e) 
        { 

         errorDialog("Exceptions"+e.toString()+e.getMessage()); 
         System.exit(1); 
         //System.exit(0);/*Kill System*/ 
        } 


       } 




     } 
     catch (Exception except) 
     {        

     } 
    } 

    public void setActive(boolean exception,String url) 
    { 
     this.hasException=exception; 
     this._theUrl=url; 

    } 

}

回答

0

如果connecrtion失败,通常情况下,你要关闭它,暂停一个小的时间,然后重试。暂停的目的是为了防止您的设备投入过多资源尝试连接到有问题的服务器。

+0

我已经在上面添加了一个线程类来检查请求中是否发生异常......这是最有效的方法吗? – 2011-05-05 12:25:15

+0

没有工作... – 2011-05-06 21:47:40