如何来ping从Android的活动网站,并得到回应? [英] How to ping a website from an Android activity and get response?
本文介绍了如何来ping从Android的活动网站,并得到回应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用isReachable,但它没有工作,我用ConnectivityManager和getNetworkInfo;其实这只能检查如果我连接到互联网...
I used isReachable, but it didn't work, and I used ConnectivityManager and getNetworkInfo; actually this works only to check if I am connected to the Internet...
但问题是我要检查,如果我能上网的,所以我想用ping一个网站,以检查是否有响应。
But the issue is I want to check if I can access the Internet so I want to ping a website to check if there is a response.
推荐答案
使用这一个.. 这正常工作对我来说:)
Use this one.. This works fine for me :)
public static void isNetworkAvailable(Context context){
HttpGet httpGet = new HttpGet("http://www.google.com");
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
try{
Log.e("checking", "Checking network connection...");
httpClient.execute(httpGet);
Log.e("checking", "Connection OK");
return;
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
Log.e("checking", "Connection unavailable");
}
这篇关于如何来ping从Android的活动网站,并得到回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文