Re: [问题]如何判断 网络 连线能力

楼主: chiel (All 喷 将!!)   2014-05-27 00:54:07
※ 引述《slemonade (淳)》之铭言:
: ※ 引述《slemonade (淳)》之铭言:
: : 请问各位前辈,当手机开启wifi or 3g,可以用ConnectivityManager获得连线资料;但是当wifi是内网且没有上网的能力(无法看网页),获得的资讯一样是有连线的;有没有办法可以知道wifi是不是确实可以上网的呢? 谢谢
: Process p= Runtime.getRumtime.exec(ping -c 1 www.google.com); p.waitfor(); 用ping如果ping不到的话,会卡住呢,请问有什么解决方式或其他方法吗?
Try it.
public static boolean ping(String url, int timeout) {
// Otherwise an exception may be thrown on invalid SSL certificates.
url = url.replaceFirst("https", "http");
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
return (200 <= responseCode && responseCode <= 399);
} catch (IOException exception) {
return false;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
至于推文提到的 walled garden 问题
http://goo.gl/dfWq7A 这个link可以看看
作者: johnpage (johnpage)   2014-05-27 04:48:00
ping 没使用timeout
作者: slemonade (淳)   2014-05-28 00:07:00
感谢大大们的方法,虽然看不太懂,不过还是试试^^

Links booklink

Contact Us: admin [ a t ] ucptt.com