检查Internet连接时出现IOException:java.net.SocketTimeoutException

时间:2017-06-15 01:33:33

标签: android

每次检查互联网连接时都会收到错误消息。是的,我已经设置了我的权限。请问我该如何处理例外情况? 我的代码和logcat如下所示:

ConnectionDetector.java:

public class ConnectionDetector {

/**
 * Checking for all possible internet providers
 * **/
public static  boolean isConnectingToInternet(Context _context){
    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)
                {
                    return true;
                }

    }
    return false;
}

}

编辑:logcat输出:

06-14 18:41:00.211 2984-14403/com.bluestacks.s2p E/S2P.: IOException while checking Internet connection: 
 java.net.SocketTimeoutException: failed to connect to www.apple.com/23.203.89.26 (port 80) after 3000ms
     at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
     at libcore.io.IoBridge.connect(IoBridge.java:112)
     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
     at java.net.Socket.connect(Socket.java:843)
     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
     at com.android.okhttp.Connection.connect(Connection.java:101)
     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
     at com.bluestacks.s2p.G.isNetworkConnected(G.java:193)
     at com.bluestacks.s2p.PingService.onHandleIntent(PingService.java:67)
     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:136)
     at android.os.HandlerThread.run(HandlerThread.java:61)

1 个答案:

答案 0 :(得分:0)

getAllNetworkInfo()自API 23以来被弃用here in Android docs

检查网络连接的更好,更清晰的方法我建议如下:

ConnectivityManager connManager =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
    // TODO: Implement functionality after successfully connecting to the network
} else {
    // TODO: Otherwise, inform the user that network connection failed
}

UPDATE:

在更详细地阅读了关于logcat错误的更新帖子之后,我必须提到,对于SocketTimeoutExceptions,你可以做的不多 - 换句话说,它很可能是互联网连接问题。也就是说,解决此问题的最佳方法是在打开并连接到HTTP服务器时捕获异常(IOException或SocketTimeoutException),然后通过UI通知用户Internet连接失败。例如:

HttpURLConnection urlConnection = null;
try {
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setReadTimeout(10000 /* milliseconds */);
    urlConnection.setConnectTimeout(15000 /* milliseconds */);
    urlConnection.setRequestMethod("GET");
    urlConnection.connect();   
} catch (IOException e) {
    Toast.makeText(this, "Problem connecting to the internet", Toast.LENGTH_SHORT).show();
    Log.e(LOG_TAG, "Problem connecting to the internet", e);
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
}