【问题标题】:How to detect internet connection with SWT browser (or handle server not available)如何使用 SWT 浏览器检测 Internet 连接(或处理服务器不可用)
【发布时间】:2011-10-27 10:13:32
【问题描述】:

我有一个带有 SWT 浏览器的 SWT Java 应用程序,它在启动时向用户显示我网站的新闻页面,同时用户也可以查看“实时”帮助文件。

但是,我需要一种方法来检查互联网是否不可用,以便显示帮助和欢迎页面的本地离线副本。

在 Mac 上,如果没有互联网,则会弹出一个对话框,说明:

“页面加载失败并出现错误:无法连接到服务器。”

我想在不将弹出窗口发送给用户的情况下处理此问题(显然这对用户来说很烦人),因此我可以在发生这种情况时切换页面。

谢谢!

【问题讨论】:

    标签: java browser swt


    【解决方案1】:

    使用Detect internet Connection using Java 的答案,您可以检查用户是否已连接到互联网,并且您的实时帮助是否已启动并正常工作。然后,如果连接中断或您的帮助出现故障,您可以显示本地版本。

    这是完整的代码(我也使用了浏览器小部件的sn-p)

    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.UnknownHostException;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.SWTError;
    import org.eclipse.swt.browser.Browser;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    
    public class BrowserWidget {
    
        private final String NET_URL = "http://eclipse.org";
        private final String LOCAL_URL = "file:///index.html";
        private String url = "";
    
        public BrowserWidget() {
            Display display = new Display();
            final Shell shell = new Shell(display);
            GridLayout gridLayout = new GridLayout();
            gridLayout.numColumns = 3;
            shell.setLayout(gridLayout);
    
            final Browser browser;
            try {
                browser = new Browser(shell, SWT.NONE);
            } catch (SWTError e) {
                System.out.println("Could not instantiate Browser: " + e.getMessage());
                display.dispose();
                return;
            }
            GridData data = new GridData();
            data.horizontalAlignment = GridData.FILL;
            data.verticalAlignment = GridData.FILL;
            data.horizontalSpan = 3;
            data.grabExcessHorizontalSpace = true;
            data.grabExcessVerticalSpace = true;
            browser.setLayoutData(data);
    
            shell.open();
    
            if(isInternetReachable()) browser.setUrl(NET_URL);
            else browser.setUrl(LOCAL_URL);
    
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
        }
    
        private boolean isInternetReachable() {
            HttpURLConnection urlConnect = null;
    
            try {
                // make a URL to a known source
                URL url = new URL(NET_URL);
                // open a connection to that source
                urlConnect = (HttpURLConnection) url.openConnection();
                // trying to retrieve data from the source. If there is no connection, this line will fail
                urlConnect.getContent();
            } catch (UnknownHostException e) {
                return false;
            } catch (IOException e) {
                return false;
            } finally {
                // cleanup
                if(urlConnect != null) urlConnect.disconnect();
            }
            return true;
        }
    
        public static void main(String[] args) {
            new BrowserWidget();
        }
    }
    

    当然,将NET_URLLOCAL_URL 常量设置为适当的网址..

    【讨论】:

      【解决方案2】:

      这就是我最终的做法:

      我的代码替换:

      ~NOINTERNETURL~ 带有“默认”本地 url(我的 SWT 浏览器检测本地 url 并显示适当的页面)

      ~INTERNETURL~如果我可以连接到我的服务器,我想去的页面

      ~INTERNETTESTURL~ 带有我服务器上脚本的 url,基本上是“var HasInternet = true”

      <html>
      <head>
      <meta http-equiv="refresh" content="5;url=~NOINTERNETURL~">
      </head>
      <body>
      <script src="~INTERNETTESTURL~" type="text/javascript"></script>
      <script type="text/javascript">
      
          if (typeof HasInternet === 'undefined') {
              window.location.href = "~NOINTERNETURL~";
          }
          else {
              window.location.href = "~INTERNETURL~";
          }
      
      </script>
      <div>
          Testing internet connection...
      </div>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-15
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-06
        相关资源
        最近更新 更多