我使用的是Apache HttpClient 4.2.5,需要设置连接超时30秒。我做了以下事情:
int timeout = 30 * 1000;
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
HttpClient client = new DefaultHttpClient(params);
HttpGet request = new HttpGet(url.toURI());
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
HttpResponse response = client.execute(request);
但是12秒后,从client.execute(请求)抛出NoRouteToHostException。据我所知,CONNECTION_TIMEOUT和SO_TIMEOUT在这里毫无用处。你有什么想法,如何为NoRouteToHostException设置超时?我希望服务器在这段时间内可用。感谢您的任何评论和建议!