如何检查网络的WiFi可用(尽管无线连接) [英] How to check internet is available in wifi(Though Wifi is connected )

查看:199
本文介绍了如何检查网络的WiFi可用(尽管无线连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code,检查网络是否可用。

I used this code to check internet is available or not..

    ConnectivityManager connectivity = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity != null) {

        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)

    if (info[i].getState() == NetworkInfo.State.CONNECTED && info[i].isAvailable())

        {
            return true;
        }
    }
    return false;

但我想要的是:

假设无线连接,但无线没有互联网...如何检查网络数据是可用的WiFi无线eventhough是connected..if我使用abouve code,它只是检查是否无线连接或断开..它会庆幸,如果一些给我一个解决方案...

Suppose wifi is connected but no internet in wifi... how to check internet data is available in wifi eventhough Wifi is connected..if i use abouve code it just checks whether wifi is connected or disconnected.. it will be thankful if some gives me a solution...

推荐答案

最简单的办法是发送HTTP请求出去,如果你的HTTP请求超时,那么你没有互联网连接,也可以检查多个响应code。当您连接到无线网络,但WiFi路由器不具备ISP活动连接时,你提到的情况。最好的办法是依靠http请求海事组织。

Easiest thing is to send http request out and if your http request times out then you don't have internet connection or you can check for more response code. The condition that you mentioned occurs when you are connected to WiFi but the WiFi router does not have active connection to ISP. The best thing is to rely on http request imo.

下面是例子code:

try
  {

   HttpGet request = new HttpGet(url));
   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 = 60000;
   HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
   // Set the default socket timeout (SO_TIMEOUT) 
   // in milliseconds which is the timeout for waiting for data.
   int timeoutSocket = 60000;
   HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
   // create object of DefaultHttpClient    
   DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
   request.addHeader("Content-Type", "application/json");
   HttpResponse response = httpClient.execute(request);
   // get response entity
   HttpEntity entity = response.getEntity();
   // convert entity response to string

     if (entity != null)
      {
         result = EntityUtils.toString(entity);
      }

   }
 catch (SocketException e)
  {
     return "-222" + e.toString();
  }
 catch (Exception e)
  {
     return "-333" + e.toString();
  }

这篇关于如何检查网络的WiFi可用(尽管无线连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆