webservice availability check in android
What should be the factors considered while checking if webservice is available or running in开发者_如何学Go android? FYI I am using HTTPGet object to send a request. I am currently checking only timeout exceptions.
Thanks..
PS Also checked android and ksoap, check web service availability but it doesn't seem to point me to a direction.
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;
}
More faster way is using HttpGet request and DefaultHttpClient:
public boolean isConnected(String url)
{
try
{
HttpGet request = new HttpGet(url);
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy()
{
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context)
{
return 0;
}
});
HttpResponse response = httpClient.execute(request);
return response.getStatusLine().getStatusCode() == 200;
}
catch (IOException e){}
return false;
}
You should probably check the HTTP Status Codes
精彩评论