URLPing urlPing = new URLPing();
PingResponse pingResponse = urlPing.ping(URLName);
if(pingResponse.getResponseCode() == 200){
response = true;
}
else{
response=false;
}
这就是我目前所尝试的。
答案 0 :(得分:4)
public static boolean pingUrl(final String address) {
try {
final URL url = new URL("http://" + address);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
final long startTime = System.currentTimeMillis();
urlConn.connect();
final long endTime = System.currentTimeMillis();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Time (ms) : " + (endTime - startTime));
System.out.println("Ping to "+address +" was success");
return true;
}
} catch (final MalformedURLException e1) {
e1.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
return false;
}
答案 1 :(得分:0)
要ping URL:
public static boolean exists()
{
try
{
return (Runtime.getRuntime().exec("/system/bin/ping -c 1 google.com").waitFor() == 0);
}
catch (IOException | InterruptedException exception)
{
exception.printStackTrace();
// Handler
}
return false;
}