I'm developping an Android REST-API oriented application.
我正在开发面向Android REST-API的应用程序。
I need to create a method to check whether the server is available or not. The problem is if you use the URL.openStream() method, there's no way to determine whether a request was successful or not.
我需要创建一个方法来检查服务器是否可用。问题是如果您使用URL.openStream()方法,则无法确定请求是否成功。
Is there a way to do it without the need to operate of performing a full HttpURLConnection and read the return code?
有没有办法在不需要执行完整的HttpURLConnection并读取返回代码的情况下执行此操作?
1 个解决方案
#1
0
-
You can use TCP Sockets
您可以使用TCP套接字
SocketAddress socketAddress = new InetSocketAddress(address, port); try { int timeout = 2000; socket.connect(socketAddress, timeout); } catch (IOException e) { return false; } finally { if (socket.isConnected()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
#1
0
-
You can use TCP Sockets
您可以使用TCP套接字
SocketAddress socketAddress = new InetSocketAddress(address, port); try { int timeout = 2000; socket.connect(socketAddress, timeout); } catch (IOException e) { return false; } finally { if (socket.isConnected()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } }