简体   繁体   中英

Cancelling Http connection in android

I am using org.apache.http and I've this code:

DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);

HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();

InputStream input = entity.getContent();

...
//Read the bytes from input stream

This is the code I am using to download files over Http, I want to cancel the connection(may be user chooses to) What is the graceful way to close the connection. I found 2 ways, Both cancels the download.

  1. Closing inputsteram, input.close(); which causes IOException.
  2. Aborting HttpGet object, get.abort() causes SocketException.

I have try catch, so no erros, but without throwing exception, is there a way to cancel or abort the connection?

What is the right way to go about it ?

The proper way doing this is sending FIN value to the server side.

How ever in android you do not have the option to be involved in this level, so you can implement by your self using C, or use one of the methods you mention in your question.

Using HttpUriRequest#about is the right way in my opinion. This will cause immediate termination of the underlying connection and its eviction from the connection pool. Newer versions of HttpClient (4.2 and newer) intercept SocketExceptions caused by premature request termination by the user. The problem is that Google ships a fork of HttpClient based on an extremely outdated version (pre-beta1). If you are not able or willing to use a newer version of HttpClient your only option is to catch and discard SocketException in your code.

Use this

client.getConnectionManager().closeExpiredConnections();
client.getConnectionManager().shutdown();

Now you can decide where would you like to write these 2 lines in code.. It will close the connection using the DefaultHttpClient object that you created.

Let me know if this helps you.

Try to cancel the task when you want to interrupt the connection: task.cancel(true); This will cancel the task and the threads running in it. Check this for reference: public final boolean cancel (boolean mayInterruptIfRunning)

Since: API Level 3 Attempts to cancel execution of this task. This attempt will fail if the task has already completed, already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns. Calling this method guarantees that onPostExecute(Object) is never invoked. After invoking this method, you should check the value returned by isCancelled() periodically from doInBackground(Object[]) to finish the task as early as possible.

Parameters mayInterruptIfRunning true if the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete. Returns false if the task could not be cancelled, typically because it has already completed normally; true otherwise

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM