所以我遇到了一个不工作的套接字超时。我遵循了现有帖子给出的所有说明,但它仍然不起作用(我从未收到套接字超时异常)。这是我的代码:
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];
try {
HttpGet httpGet = new HttpGet(location);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
try {
// doStuff
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);
所以我应该在两秒后收到套接字超时异常,但客户端只是忽略了这一点。有什么帮助吗?
提前致谢
所以这里是所有代码:
public void fetch(String location) {
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];
try {
HttpGet httpGet = new HttpGet(location);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String result) {
try {
//doStuff
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);
}
不久前我遇到了类似的问题。我做了一些实验,注意到当我在模拟器中运行我的应用程序时,超时不起作用,当我在真实设备上运行它时,它确实有效。我已经用DefaultHttpClient
、HttpUrlConnection
和AndroidHttpClient
进行了测试,这三个都显示出相同的结果;在模拟器中大约 20 秒后出现 IO异常 (UnknownHostException),无论设置的超时如何。
谷歌搜索显示,其他人也报告了超时问题:
- http://code.google.com/p/android/issues/detail?id=2409
- 安卓上的 http 连接超时不起作用
- HTTP 连接超时问题
- 使用 DefaultHTTPClient 发出 HTTPGet 请求时的套接字超时
提出的解决方案都不适合我,所以我想唯一可靠的解决方案是自己管理超时。
参见:https://stackoverflow.com/a/20031077/2609238
问题可能出在 Apache HTTP 客户端中。请参阅 HTTPCLIENT-1098。在 4.1.2 中修复。
超时异常会出于日志记录目的尝试反向 DNS IP。这需要额外的时间,直到实际触发异常。