android如何设置连接超时啊? 100
我在程序里面是这样设置的:HttpClienthttpClient=newDefaultHttpClient();HttpParamsparams=httpClient....
我在程序里面是这样设置的:
HttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
我希望是当访问网站超过3s或者建立连接超过5s,就停止访问。但是好像结果是,当移动信号很微弱时,会过好长一段时间,大概是1-2分钟,才会网络访问结束。难道是我这样设置的不对吗?那个3s和5s参数没有生效?还是什么其他的原因?请大神指点! 展开
HttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
我希望是当访问网站超过3s或者建立连接超过5s,就停止访问。但是好像结果是,当移动信号很微弱时,会过好长一段时间,大概是1-2分钟,才会网络访问结束。难道是我这样设置的不对吗?那个3s和5s参数没有生效?还是什么其他的原因?请大神指点! 展开
1个回答
展开全部
您好,我是这样设置的,代码如下:
public static InputStream getHttpByApache(String urlStr) throws Exception {
HttpGet httpGet = new HttpGet(urlStr);
HttpClient httpClient = new DefaultHttpClient();
// 设置连接超时、读取超时
HttpParams basicHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(basicHttpParams, 3 * 1000);
HttpConnectionParams.setSoTimeout(basicHttpParams, 3 * 1000);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return httpResponse.getEntity().getContent();
}
return null;
}
public static InputStream getHttpByApache(String urlStr) throws Exception {
HttpGet httpGet = new HttpGet(urlStr);
HttpClient httpClient = new DefaultHttpClient();
// 设置连接超时、读取超时
HttpParams basicHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(basicHttpParams, 3 * 1000);
HttpConnectionParams.setSoTimeout(basicHttpParams, 3 * 1000);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return httpResponse.getEntity().getContent();
}
return null;
}
追问
你这个生效了吗?好像basicHttpParams都没有绑定到httpClient上。
追答
您试试不就知道了么,编程就是不怕报错不是吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询