本文介绍了检查真正的互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序需要检查是否设备连接到互联网。我试图用ConnectivityManager,但它并没有给出100%precise结果。举例来说,我可能有一个Wi-Fi连接,但仍无法访问互联网资源。在我来说,我一定要打开VPN连接后,我已经通过Wi-Fi连接,以获得真正进入互联网。因此,与ConnectivityManager的方法是行不通的。

所以,关于上述 - 我应该写文章是为了手工HTTP请求,以确保或有另一种方式

下面是一些code我用

  ConnectivityManager厘米=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        返回cm.getActiveNetworkInfo()= NULL和放大器;!&安培; cm.getActiveNetworkInfo()isConnected()&安培;&安培; 。cm.getActiveNetworkInfo()isAvailable();
 

解决方案

而不是检查互联网连接的每一次,我想设置 ConnectionTimeout 来HTT prequest其最好的办法,

 尝试
  {

   HTTPGET请求=新HTTPGET(URL));
   的HttpParams httpParameters =新BasicHttpParams();
   //毫秒设置超时,直到建立了连接。
   //默认值是零,这意味着在超时不被使用。
   INT timeoutConnection = 60000;
   HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
   //设置默认套接字超时(SO_TIMEOUT)
   //毫秒这是超时等待数据。
   INT timeoutSocket = 60000;
   HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
   //创建DefaultHttpClient对象
   DefaultHttpClient的HttpClient =新DefaultHttpClient(httpParameters);
   request.addHeader(内容类型,应用/ JSON);
   HTT presponse响应= httpClient.execute(要求);
   //得到响应实体
   HttpEntity实体= response.getEntity();
   //转换实体响应串

     如果(实体!= NULL)
      {
         结果= EntityUtils.toString(实体);
      }

   }
 赶上(SocketException E)
  {
     返回-222+ e.toString();
  }
 赶上(例外五)
  {
     返回-333+ e.toString();
  }
 

In my application I need to check whether device is connected to Internet. I tried using ConnectivityManager but it doesn't give a 100% precise result. For instance, I might have a wi-fi connection but still don't have access to internet resources. In my case I've got to open a VPN connection, after I've connected to via wi-fi, in order to get real access to Internet. So the approach with ConnectivityManager doesn't work.

So, regarding the above - should I write a manual http request in order to ensure or there's another way ?

Here's some code I'm using

ConnectivityManager cm =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected() && cm.getActiveNetworkInfo().isAvailable();
解决方案

Instead of checking every time for internet connection, I think setting ConnectionTimeout to HTTPRequest its the best way,

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();
  }

这篇关于检查真正的互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:25