web服务可用性检查android

nil*_*ile 3 android web-services

检查webservice是否可用或在android中运行时应考虑的因素是什么?仅供参考我正在使用HTTPGet对象发送请求.我目前只检查超时异常.

谢谢..

PS还检查了android和ksoap,检查网络服务的可用性,但它似乎并没有指向我的方向.

Bob*_*son 8

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;
}
Run Code Online (Sandbox Code Playgroud)