android - android中的webservice可用性检查

标签 android web-services

在检查 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/

相关文章:

android - monkeyrunner 触摸事件被识别为长按

java - 接收多部分表单数据 - Jersey 1.19.1 REST

使用 JAXB 将 Java 转换为 XML

web-services - 我应该拒绝比预期更长的 URL 吗?

java - Jboss Web服务:Error :java. io.IOException:无法传输消息

android - "?"附近的 execSQL SQLiteException

Android 异常 getCause - getMessage

三星 Galaxy Tab 4 系列的 Androidmanifest.xml Play 商店设置

web-services - 使用soapui进行WSDL/SOAP测试

Android 资源$NotFoundException : Resource ID #0x0 Issue