【问题标题】:How to handle internet connection down in GWT application如何在 GWT 应用程序中处理 Internet 连接中断
【发布时间】:2014-02-21 01:47:46
【问题描述】:

我处理一个用 GWT 设计的网站,我想检查在访问该网站之间互联网连接是否中断。如果互联网出现故障,我想发送消息,因为无法连接到服务器或 Gmail 之类的东西可以处理它。

任何人都可以建议处理此问题的最佳方法吗?

【问题讨论】:

    标签: gwt


    【解决方案1】:

    这就是AsyncCallback 上的onFailure(Throwable t) 方法的用途。当 RPC 因任何原因失败时调用此方法,包括(但不限于)连接丢失。

    由于传递给onFailure()Throwable 可以是任何东西,因此文档中使用的模式是:

    public void onFailure(Throwable caught) {
      // Convenient way to find out which exception was thrown.
      try {
        throw caught;
      } catch (IncompatibleRemoteServiceException e) {
        // this client is not compatible with the server; cleanup and refresh the 
        // browser
      } catch (InvocationException e) {
        // the call didn't complete cleanly
      // other Throwables may be caught here...
      } catch (Throwable e) {
        // last resort -- a very unexpected exception
      }
    }
    

    具体来说,缺少互联网连接会导致InvocationException 被传递给onFailure()

    【讨论】:

    • 你为什么要重新抛出caught然后抓住它而不是if (caught instanceof InvocationException) { ... } ...。您是否有指向您提到的文档的链接,该链接解释了为什么推荐这种模式?
    • 无论哪种方式都可以,这只是一个偏好问题。这就是他们在示例中使用的模式。如果忘记添加else 语句,重新抛出它的好处是不会丢失异常。
    • 感谢杰森的回复。我刚刚使用了 if(caught instanceof InvocationException){..} 它对我有用。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多