Android HttpURLConnection setConnectTimeout不工作
我正在使用HttpURLConnection与应用程序中的服务器通信。但我发现,如果我的android设备连接不好,我的应用程序将挂起很长时间,这是一种糟糕的用户体验。我同时使用setConnectTimeout()和setReadTimeout(),但它似乎不起作用。下面是我的代码:Android HttpURLConnection setConnectTimeout不工作,android,httpurlconnection,Android,Httpurlconnection,我正在使用HttpURLConnection与应用程序中的服务器通信。但我发现,如果我的android设备连接不好,我的应用程序将挂起很长时间,这是一种糟糕的用户体验。我同时使用setConnectTimeout()和setReadTimeout(),但它似乎不起作用。下面是我的代码: URL url = null; HttpURLConnection connection = null; InputStreamReader ins = null;
URL url = null;
HttpURLConnection connection = null;
InputStreamReader ins = null;
try {
url = new URL("XXXXXXXXXXXXXXXXX");
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
ins = new InputStreamReader(connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(ins);
StringBuffer strBuffer = new StringBuffer();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
strBuffer.append(line);
}
result = strBuffer.toString();
line = null;
strBuffer = null;
bufferedReader = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
connection = null;
}
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
ins = null;
}
url = null;
}
从我的代码中我们可以看到,当我的internet连接不良时,我同时使用setConnectTimeout()和setReadTimeout()。但是,它仍然会挂起很长一段时间(超过30秒甚至更长),最终会显示这样的日志:
07-17 13:27:47.200: W/System.err(18005): java.net.UnknownHostException: Unable to resolve host "server.ibaobeimao.com": No address associated with hostname
07-17 13:27:47.200: W/System.err(18005): at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
07-17 13:27:47.200: W/System.err(18005): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-17 13:27:47.200: W/System.err(18005): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-17 13:27:47.200: W/System.err(18005): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
07-17 13:27:47.203: W/System.err(18005): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-17 13:27:47.207: W/System.err(18005): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
07-17 13:27:47.207: W/System.err(18005): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-17 13:27:47.207: W/System.err(18005): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-17 13:27:47.207: W/System.err(18005): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
07-17 13:27:47.207: W/System.err(18005): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
07-17 13:27:47.210: W/System.err(18005): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
07-17 13:27:47.210: W/System.err(18005): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
07-17 13:27:47.210: W/System.err(18005): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
07-17 13:27:47.213: W/System.err(18005): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
07-17 13:27:47.213: W/System.err(18005): at utils.Rpc.httpRequest(Rpc.java:49)
07-17 13:27:47.213: W/System.err(18005): at utils.Utils$11.run(Utils.java:1154)
07-17 13:27:47.213: W/System.err(18005): at java.lang.Thread.run(Thread.java:856)
07-17 13:27:47.213: W/System.err(18005): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
07-17 13:27:47.217: W/System.err(18005): at libcore.io.Posix.getaddrinfo(Native Method)
07-17 13:27:47.217: W/System.err(18005): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:60)
07-17 13:27:47.217: W/System.err(18005): at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
07-17 13:27:47.217: W/System.err(18005): ... 16 more
07-17 13:27:47.217: W/System.err(18005): Caused by: libcore.io.ErrnoException: getaddrinfo failed: ETIMEDOUT (Connection timed out)
07-17 13:27:47.220: W/System.err(18005): ... 19 more
但无论我是否真的上网,我的结果总是假的。谁能给我一些建议吗?不幸的是,你的问题出在较低的网络层。您的参数仅在请求HTTP时起作用。你被困在解析DNS上。 事件顺序:
- 解析dns
- 建立TCP连接
- 发送HTTP请求
- 接收HTTP响应
我唯一能想到的解决方法就是做一个连接检查,比如打开与google gen_204的连接。你试过让它异步吗?谢谢你的评论。我会尽快从你给我的网站上试试。谢谢你的回答~我已经读过了,这是不是意味着我需要ping一个网站,比如google.com,来检查我是否真的可以连接到互联网?是的。否则,系统将在放弃之前尝试解析dns一段时间。
public static final boolean ping() {
String result = null;
try {
String ip = "202.108.22.5"; // ping address
Process p = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 10 " + ip); // ping 3 times
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
}
// ping's situation
int status = p.waitFor();
if (status == 0) {
result = "success";
return true;
} else {
result = "failed";
}
} catch (IOException e) {
result = "IOException";
} catch (InterruptedException e) {
result = "InterruptedException";
} finally {
Log.d("----result---", "result = " + result);
}
return false;
}