有没有办法使用 Java 来检测网络损坏或网络较弱?
最佳答案
您可以间歇性地测试与已知主机(例如 google)的 http 连接并检测 ConnectionExceptions
URLConnection con = new URL("http://www.google.com/").openConnection();
con.setUseCaches(false);
con.setAllowUserInteraction(false);
while(true)
{
try
{
con.connect();
con.getContentType(); // just to make sure the connection works
// sleep for some interval
}
catch(IOException e)
{
// handle and break
}
}
关于java - 如何使用java程序检测网络中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2069961/