我正在做一个项目,需要测试android和移动网络的一些功能。其中之一是检查互联网连接的可靠性。为此,我应该先检查互联网连接本身,然后再检查可靠性
有没有什么算法或库可以用来确定Internet连接的可靠性
发布于 2016-08-17 19:53:20
也许你可以向一个已知的站点发送一个请求,检查互联网连接是否足够稳定,以便在给定的时间内返回响应。
public boolean checkInternet(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url
.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
// Means response is available within 3 seconds
// So, return true or do something else.
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}https://stackoverflow.com/questions/38994230
复制相似问题