How to solve error “Invalid use of BasicClientConnManager : Make sure to release the connection before allocating another one” in Android?

ⅰ亾dé卋堺 提交于 2019-12-05 09:17:25

When you create a DefaultHttpClient with specify any ClientConnectionManager it will create by default a BasicClientConnectionManager and if you check the javadoc says:

A connection manager for a single connection. This connection manager maintains only one active connection at a time. Even though this class is thread-safe it ought to be used by one execution thread only. BasicClientConnManager will make an effort to reuse the connection for subsequent requests with the same route. It will, however, close the existing connection and open it for the given route, if the route of the persistent connection does not match that of the connection request. If the connection has been already been allocated IllegalStateException is thrown.

Try setting a PoolingClientConnectionManager with custom configuration for your needs. See a simple example:

PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager( SchemeRegistryFactory.createDefault());
cxMgr.setMaxTotal(100);
cxMgr.setDefaultMaxPerRoute(20);

This is valid for HttpClient 4.2 since ThreadSafeClientConnManager was deprecated for that version. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!