Blackberry如果失败,最好重试连接?

时间:2011-05-04 13:34:31

标签: java api networking blackberry tcp

我有以下代码,我希望能够在处理请求时发生异常时重启线程。

以下在线程的run方法中:

int status = 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 {         private static final int 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;

    }

}

1 个答案:

答案 0 :(得分:0)

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