Android:检查与服务器的连接

发布于 2025-01-02 22:13:40 字数 221 浏览 1 评论 0原文

所以我有一种方法可以检查设备是否连接到互联网基于此

它工作正常,但在某些情况下,我发现虽然手机显示已连接到互联网,但它仍然无法访问我的服务器。我真正想做的是检查与我的特定 URL 的连接。

我该如何实现呢?

So I have a method that checks if the device is connected to the internet based on this one

It works ok, but in a couple of cases I find that although the phone shows being connected to the internet, it still does not have access to my server. What I am really looking to do it check connection to my specific URL.

How could I implement that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦行七里 2025-01-09 22:13:40

您可以执行以下操作:

public boolean isConnectedToServer(String url, int timeout) {
    try{
        URL myUrl = new URL(url);
        URLConnection connection = myUrl.openConnection();
        connection.setConnectTimeout(timeout);
        connection.connect();
        return true;
    } catch (Exception e) {
        // Handle your exceptions
        return false;
    }
}

这将尝试连接到您的服务器的 URL,如果失败,则显然您没有连接。 ;-)

You could do something like:

public boolean isConnectedToServer(String url, int timeout) {
    try{
        URL myUrl = new URL(url);
        URLConnection connection = myUrl.openConnection();
        connection.setConnectTimeout(timeout);
        connection.connect();
        return true;
    } catch (Exception e) {
        // Handle your exceptions
        return false;
    }
}

This will attempt to connect to your server's URL and if it fails, you obviously don't have a connection. ;-)

墨落画卷 2025-01-09 22:13:40

你可以尝试

InetAddress.getByName(host).isReachable(timeOut)

You can try

InetAddress.getByName(host).isReachable(timeOut)
じ违心 2025-01-09 22:13:40
Runtime runtime = Runtime.getRuntime();
            Process proc;
            try {
                proc = runtime.exec("ping -c 1"+ (String) getText(R.string.Server));
                proc.waitFor();
                int exit = proc.exitValue();
                if (exit == 0) { // normal exit


                } else { // abnormal exit, so decide that the server is not
                            // reachable


                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // other servers, for example
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Runtime runtime = Runtime.getRuntime();
            Process proc;
            try {
                proc = runtime.exec("ping -c 1"+ (String) getText(R.string.Server));
                proc.waitFor();
                int exit = proc.exitValue();
                if (exit == 0) { // normal exit


                } else { // abnormal exit, so decide that the server is not
                            // reachable


                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // other servers, for example
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文