Android 检查Internet连接时发生IOException:java.net.SocketTimeoutException

Android 检查Internet连接时发生IOException:java.net.SocketTimeoutException,android,Android,我每次检查internet连接时都会收到错误。是的,我已经设置了权限。请问我如何处理异常? 下面提供了我的代码和日志: ConnectionDetector.java: public class ConnectionDetector { /** * Checking for all possible internet providers * **/ public static boolean isConnectingToInternet(Context _context){ Co

我每次检查internet连接时都会收到错误。是的,我已经设置了权限。请问我如何处理异常?
下面提供了我的代码和日志:

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;
}
如前所述,自API 23以来,
getAllNetworkInfo()
已被弃用

我推荐一种更好、更干净的网络连接检查方法如下:

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
}
更新: 在更详细地阅读了您关于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();
    }
}

如果有例外情况,请在问题中包含例外情况的详细信息。很抱歉,因为我是新来的。下面是日志:谢谢DaveNOTDavid,但我在android开发方面相对较新。为了更好地测试和学习,我学习了在线教程。但到目前为止,我还没能克服这个困难error@AceOgbonnaya好的,让我知道进展如何。另外,我再次更新了帖子:-)我是否将此代码放在ConnectorDetector.java文件中?请帮助编辑下面的NewsFeedParser方法:public NewsFeedParser(字符串urlString){this.urlString=urlString;}public static InputStream downloadStream(字符串urlString)抛出IOException{URL URL URL=new URL(urlString);HttpURLConnection conn=(HttpURLConnection)url.openConnection();conn.setRequestMethod(“GET”);conn.setDoInput(true);conn.connect();InputStream=conn.getInputStream();返回流;}或者,请找到该项目的链接,并帮助我找出处理异常的方法:
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();
    }
}