开发者

How to detect internet connection with SWT browser (or handle server not available)

开发者 https://www.devze.com 2023-03-28 10:05 出处:网络
I have a SWT Java application with a SWT Browser that is displayed on startup to display the news page of my website to the user, and also so the user can view \"live\" help files.

I have a SWT Java application with a SWT Browser that is displayed on startup to display the news page of my website to the user, and also so the user can view "live" help files.

However, I need a way to check if internet is not available so I can display a local, offline copy of the help and welcome pages.

On Mac a dialog pops up if there is no internet, stating:

"Page load failed with error: Could not connect to the server."

I'd like to handle this without the popup going to the user (obviously thi开发者_StackOverflows is annoying to the user) and so I can switch the page when this happens.

Thanks!


Using answer for Detect internet Connection using Java you can check whether is user connected to internet and more alike your live help is up and working. Then, if connection is broken or your help is out of order, you can show the local version.

Here is complete code (I also used snippet of browser widget)

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();
    }
}

Of course, set the NET_URL and LOCAL_URL constants to appropriate URLs..


Here's how I ended up doing it:

My code replaces:

~NOINTERNETURL~ with the 'default' local url (my SWT browser detects local urls and displays appropriate pages)

~INTERNETURL~ with the page I want to go to if I can connect to my server

~INTERNETTESTURL~ with the url of a script on my server which basically is "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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号