Android HttpClient出现org.apache.http.conn.ConnectTimeoutException,导致下载不成功如何解决

zhoulc 发布于 2013/05/15 10:15
阅读 27K+
收藏 0
在Android 4.0环境下,应用使用HttpClient向服务请求xml数据或者请求图片下载,出现下列异常(请求xml数据部分代码没有给出)
W/System.err( 2770): org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:353)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:238)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:177)
W/System.err( 2770):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.downFile(DownLoadToolUtil.java:108)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.run(DownLoadToolUtil.java:64)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err( 2770):    at java.lang.Thread.run(Thread.java:856)
I/CategoryActivity( 2770): action:IMG_DOWNLOAD_COMPLETE
D/DetailActivity( 2770): ####zhoulc has getMessage,action:IMG_DOWNLOAD_COMPLETE
W/System.err( 2770): org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:353)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:238)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:177)
W/System.err( 2770):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.downFile(DownLoadToolUtil.java:108)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.run(DownLoadToolUtil.java:64)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err( 2770):    at java.lang.Thread.run(Thread.java:856)
W/System.err( 2770): org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:353)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:238)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:177)
W/System.err( 2770):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.downFile(DownLoadToolUtil.java:108)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.run(DownLoadToolUtil.java:64)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err( 2770):    at java.lang.Thread.run(Thread.java:856)
D/skia    ( 2770): --- decoder->decode returned false
W/System.err( 2770): org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:353)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:238)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:177)
W/System.err( 2770):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.downFile(DownLoadToolUtil.java:108)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.run(DownLoadToolUtil.java:64)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err( 2770):    at java.lang.Thread.run(Thread.java:856)
W/System.err( 2770): org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:353)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:238)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:177)
W/System.err( 2770):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.downFile(DownLoadToolUtil.java:108)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.run(DownLoadToolUtil.java:64)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err( 2770):    at java.lang.Thread.run(Thread.java:856)
W/System.err( 2770): org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:353)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:238)
W/System.err( 2770):    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:177)
W/System.err( 2770):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 2770):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.downFile(DownLoadToolUtil.java:108)
W/System.err( 2770):    at com.ipanel.market.request.DownLoadToolUtil.run(DownLoadToolUtil.java:64)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W/System.err( 2770):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W/System.err( 2770):    at java.lang.Thread.run(Thread.java:856)
具体下载部分代码如下:
public int downFile(String fileName, String url, long curfilelength) {
		InputStream input = null;
		RandomAccessFile file = null;
		int readSize = -1;
		byte[] buffer = new byte[8024];
		Log.d(TAG, "downFile url = " + url);
		Log.d(TAG, "downFile fileName = " + url);
		Log.d(TAG, "downFile curfilelength = " + curfilelength);
		try {
			if (curfilelength != 0) {
				url = FilePath.GET_INTERRUPT_APK + "?url=" + url + "&fileSize="
						+ curfilelength;
				Log.d(TAG, "after change" + url);
			}
			HttpGet request = new HttpGet();
//			request.addHeader("Range", "bytes=" + curfilelength + "-");
			request.setURI(new URI(url));
			HttpResponse response = client.execute(request);
			file = new RandomAccessFile(fileName, "rw");
			file.seek(curfilelength);
			input = response.getEntity().getContent();
			while ((readSize = input.read(buffer)) != -1) {
				file.write(buffer, 0, readSize);
			}
			return DownLoadCallBack.DOWNLOAD_SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return DownLoadCallBack.DOWNLOAD_FAILD;
		} finally {
			if (input != null) {
				try {
					input.close();
					input = null;
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			if (file != null) {
				try {
					file.close();
					file = null;
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}

	}
HttpClient也设置了超时情况:
//设置连接管理器的超时
			ConnManagerParams.setTimeout(params, 1000);
			//设置连接超时
			HttpConnectionParams.setConnectionTimeout(params, 5000);
			//设置socket超时
			HttpConnectionParams.setSoTimeout(params, 10000);
请问,这个异常是一定会抛的吗,导致图片没有下载成功,我需要在处理异常的时候再次进行下载,还是代码哪有缺陷,需要改正。
加载中
0
blackylin
blackylin

引用来自“zhoulc”的答案

问题解决了,在做耗时较长的http请求交互的时候,重新new一个httpClient对象,而不是用一个单例的httpclient对象进行管理,所有的http请求。
一般的请求可以用单例的httpClient,但是耗时较长的资源请求或网络带宽被其他程序占用,都可能会出现这个异常。重新new 一个httpClient对象可以暂时解决,最好超时延长试试
0
loki_lan
loki_lan
ConnManagerParams.setTimeout这个是连接最大等待时间,HttpConnectionParams.setConnectionTimeout这个是最大连接超时时间,HttpConnectionParams.setSoTimeout这个是读取超时时间,根据org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection,可以知道是在ConnManagerParams.setTimeout(params, 1000);这句抛出的,把1000改成10000吧。
zhoulc
zhoulc
sorry,多测试几次之后,还是出现了这个问题。
0
zhoulc
zhoulc

引用来自“铂金小猫”的答案

ConnManagerParams.setTimeout这个是连接最大等待时间,HttpConnectionParams.setConnectionTimeout这个是最大连接超时时间,HttpConnectionParams.setSoTimeout这个是读取超时时间,根据org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection,可以知道是在ConnManagerParams.setTimeout(params, 1000);这句抛出的,把1000改成10000吧。
谢谢你的回答,异常是在 HttpResponse response = client.execute(request);的时候抛出来的,按照你的方法,把链接最大等待时间改成10秒之后,不再报链接异常了,不过cpu占用率非常高。。。
0
zhoulc
zhoulc
问题解决了,在做耗时较长的http请求交互的时候,重新new一个httpClient对象,而不是用一个单例的httpclient对象进行管理,所有的http请求。
0
d
dondy
请问这个问题,您怎么解决的?我今天也遇到了。一开始访问是ok的,偶尔会出现这个异常。。。
霜林秋山
霜林秋山
回复 @飞车兔 : 我不知道你怎么想的,要是有上万链接,岂不是很不效率了?
飞车兔
飞车兔
把连接超时的时间调大些.比如设置成60000毫秒.
返回顶部
顶部