I'm using ThreadSafeClientConnManager to manage a pool of client connections, because my application has several threads, which are simultaneously connecting to a webserver.
Abstract sample code:
HttpClient httpClient;
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(parameters,schReg);
httpclient = new DefaultHttpClient(conMgr, parameters);
Now lets say on of this threads is downloading a large file, but then the user of my application is switching to another activity/screen. Therefor the file is needless and I'd like to abort this download connection.
In ThreadSafeClientConnManager I found this method:
public ClientConnectionRequest requestConnection (HttpRoute route, Object state)
Returns a new ClientConnectionRequest, from which a ManagedClientConnection can be obtained or the request can be aborted.
So far I've been using:
HttpGet httpRequest = new HttpGet(URL_TO_FILE);
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
[...]
Now from what I understand, I've to use:
httpclient.getConnectionManager().requestConnection(HttpRoute route, Object state);
And that's the point where I'm stuck. I assume that for the route I can just use new HttpRoute(new HttpHost("10.0.0.1"))
or whatever my server is, but what to put in for Object state
?
And second, as soon as I've the ClientConnectionManager
I can call getConnection(long timeout, TimeUnit tunit)
. But then from there, how I do I execute my HttpGet httpRequest = new HttpGet(URL_TO_FILE);
as I did before with HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
?
I've been gone through the documentation and tried out quite a few different things, but I wasn't able to obtain a working solution. Therefor any suggestions and/or code examples are more than welcome.