简体   繁体   English

Blackberry 如果失败重试连接的最佳方法?

[英]Blackberry Best way to retry connection if failed?

I have the following code, i want to be able to restart the thread if an exception occurred while processing a request.我有以下代码,如果在处理请求时发生异常,我希望能够重新启动线程。

The following in the run method of a thread:线程的run方法中的以下内容:

int status = httpConn.getResponseCode(); 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...");
                }

What is the safest way to retry the connection if failed?如果失败,重试连接的最安全方法是什么?

Thanks.谢谢。

//New This is the Error thread. //New 这是错误线程。 That check for errors in the connection... will this help?检查连接中的错误...这有帮助吗? and is it the most efficient method?它是最有效的方法吗? thanks..谢谢..

/ Error Thread - Thread to check errors / private class ErrorThread extends Thread { private static final int TIMEOUT = 3000; /错误线程 - 检查错误的线程/ private class ErrorThread extends Thread { private static final int TIMEOUT = 3000; // EVERY 3 Seconds private boolean hasException = false; // 每 3 秒私有 boolean hasException = false; private String _theUrl;私有字符串_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;

    }

} }

If the connecrtion fails, typically, you want to close it, pause a small time, and retry.如果连接失败,通常需要关闭它,暂停一小段时间,然后重试。 The purpose of the pause is to prevent your device from devoting excessive resources to trying to connect to a server that's having issues.暂停的目的是防止您的设备投入过多资源来尝试连接有问题的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM