在检查 web 服务是否可用或在 android 中运行时应该考虑哪些因素?仅供引用,我正在使用 HTTPGet 对象发送请求。我目前只检查超时异常。
谢谢..
PS 还检查了 android and ksoap, check web service availability但它似乎并没有给我指明方向。
最佳答案
public boolean isConnected()
{
try{
ConnectivityManager cm = (ConnectivityManager) getSystemService
(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{
//Network is available but check if we can get access from the network.
URL url = new URL("http://www.Google.com/");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(2000); // Timeout 2 seconds.
urlc.connect();
if (urlc.getResponseCode() == 200) //Successful response.
{
return true;
}
else
{
Log.d("NO INTERNET", "NO INTERNET");
return false;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
关于android - android中的webservice可用性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6507535/