我在我的代码中使用URLConnection
。我已将ReadTimeout
设置为5秒,但不会生效。当我切换无线接入点时,IO Exception正在被称为immidiately。URLConnection不尊重ReadTimeout
URLConnection conn;
conn = new URL(StringUrls[0]).openConnection();
conn.setReadTimeout(5000);
in = conn.getInputStream();
try {
len = in.read(buffer);
bufOutstream.write(buffer, 0, len);
bufOutstream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
IO例外:
12-17 12:41:35.332 12761-13268/com.app.example W/System.err﹕ java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at libcore.net.http.UnknownLengthHttpInputStream.read(UnknownLengthHttpInputStream.java:41)
12-17 12:41:35.362 12761-13268/com.app.example W/System.err﹕ at java.io.InputStream.read(InputStream.java:163)
虽然这并不能完全回答我的问题,但我决定接受它,因为它看起来像是一个合理的解决方法。 – Kristopher