Android:在webview中检查互联网连接

时间:2014-01-06 13:55:17

标签: android android-webview internet-connection

每次启动应用程序时,我都会尝试在网页视图中测试互联网连接。

我尝试调整我发现的一个例子,但它崩溃了。 无论我的连接状态如何,INFO都会返回相同的结果

01-06 13:31:09.665: D/INFO(1738): NetworkInfo: type: MOBILE[UMTS], state: CONNECTED/CONNECTED, reason: simLoaded, extra: internet, roaming: false, failover: false, isAvailable: true
01-06 13:31:09.665: D/CheckNetwork(1738):  internet connection
01-06 13:34:18.829: W/System.err(1738): java.net.SocketException: The operation timed out
01-06 13:34:18.836: W/System.err(1738):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:114)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:245)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:535)
01-06 13:34:18.836: W/System.err(1738):     at java.net.Socket.connect(Socket.java:1054)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:129)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
01-06 13:34:18.836: W/System.err(1738):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-06 13:34:18.846: W/System.err(1738):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

ConnectivityManager connmgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connmgr != null){
          NetworkInfo[] netInfo = connmgr.getAllNetworkInfo();
          if (netInfo != null){
              for (int i = 0; i < netInfo.length; i++){
                  if (netInfo[i].getState() == NetworkInfo.State.CONNECTED){
                      // you are connected
                  }
              }
          }
      }