java.net.SocketTimeoutException:连接失败

时间:2022-09-18 17:26:46


I hava a working xmlparser
But unfortunately i get an error when i'm on mobile data.
The problem doesn't occur on Wi-Fi and the page is available at any time.
What do i do wrong?
I've the permission for Internet.

我有一个工作的xmlparser,但不幸的是,当我在移动数据上时,我得到了一个错误。这个问题不会出现在Wi-Fi上,而且网页随时都可以使用。我做错了什么?我得到了互联网的许可。

Stack trace:

堆栈跟踪:

    05-14 21:26:35.628: W/System.err(17691): java.net.SocketTimeoutException: failed to connect to buie.vlsolutions.nl/185.10.98.4 (port 80) after 15000ms
    05-14 21:26:35.633: W/System.err(17691):    at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
    05-14 21:26:35.633: W/System.err(17691):    at libcore.io.IoBridge.connect(IoBridge.java:112)
    05-14 21:26:35.633: W/System.err(17691):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
    05-14 21:26:35.633: W/System.err(17691):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
    05-14 21:26:35.633: W/System.err(17691):    at java.net.Socket.connect(Socket.java:842)
    05-14 21:26:35.633: W/System.err(17691):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
    05-14 21:26:35.638: W/System.err(17691):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
    05-14 21:26:35.638: W/System.err(17691):    at nl.vlsolutions.buienl.MainActivity.downloadUrl(MainActivity.java:475)
    05-14 21:26:35.638: W/System.err(17691):    at nl.vlsolutions.buienl.MainActivity.loadXmlFromNetwork(MainActivity.java:342)
    05-14 21:26:35.638: W/System.err(17691):    at nl.vlsolutions.buienl.MainActivity.access$2(MainActivity.java:334)
    05-14 21:26:35.643: W/System.err(17691):    at nl.vlsolutions.buienl.MainActivity$DownloadXmlTask.doInBackground(MainActivity.java:307)
    05-14 21:26:35.643: W/System.err(17691):    at nl.vlsolutions.buienl.MainActivity$DownloadXmlTask.doInBackground(MainActivity.java:1)
    05-14 21:26:35.643: W/System.err(17691):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    05-14 21:26:35.643: W/System.err(17691):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    05-14 21:26:35.643: W/System.err(17691):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    05-14 21:26:35.643: W/System.err(17691):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    05-14 21:26:35.643: W/System.err(17691):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    05-14 21:26:35.643: W/System.err(17691):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    05-14 21:26:35.648: W/System.err(17691):    at java.lang.Thread.run(Thread.java:856)

Function downloadUrl:

函数downloadUrl:

private InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();     // Line 475
    return conn.getInputStream();
}

2 个解决方案

#1


3  

It turns out there was an problem with the DNS Server from my data provider. Because my server just changed IP-addresses and the DNS Server was not yet updated.

事实证明,我的数据提供商的DNS服务器出了问题。因为我的服务器只是更改了ip地址,而DNS服务器还没有更新。

#2


1  

Have you tried to increase the connection time out? maybe your mobile data connection is too slow in contrary to your wifi. use try and catch and post the exception here, I think it'll be more helpful than the logcat in this case.

你试过增加连接时间吗?也许你的移动数据连接速度太慢,与你的wifi不一样。使用try和catch并在这里发布异常,我认为在这种情况下它将比logcat更有帮助。

#1


3  

It turns out there was an problem with the DNS Server from my data provider. Because my server just changed IP-addresses and the DNS Server was not yet updated.

事实证明,我的数据提供商的DNS服务器出了问题。因为我的服务器只是更改了ip地址,而DNS服务器还没有更新。

#2


1  

Have you tried to increase the connection time out? maybe your mobile data connection is too slow in contrary to your wifi. use try and catch and post the exception here, I think it'll be more helpful than the logcat in this case.

你试过增加连接时间吗?也许你的移动数据连接速度太慢,与你的wifi不一样。使用try和catch并在这里发布异常,我认为在这种情况下它将比logcat更有帮助。