0

I have read many answers, the proper way may be setting a flag in while loop, when flag become false the thread will stop.

public void run() {
        // TODO Auto-generated method stub
        while(flag){
            doSomething...
        }
}

But here I have a http request wrapped in the run method, there is no such while loop in my code, how to stop it properly, is it ok if I only stop the thread, or I have to do more to stop httprequest?

withparadox2
  • 422
  • 4
  • 20

2 Answers2

1
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));

now if you wanna shutdown your request use this syntax

httpclient.getConnectionManager().shutdown();
QuokMoon
  • 4,387
  • 4
  • 26
  • 50
0

Why not using AsyncTask? This way you can call mTask.cancel(); to stop your request / whatever you run in this task.

Check out this: http://developer.android.com/reference/android/os/AsyncTask.html There you can also find an example.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
Terel
  • 3,857
  • 1
  • 25
  • 28
  • You have misspelled cancel and you answer is not entirely true. It will only TRY to cancel the ASYNC TASK. There is no guaranty that the http request will terminate. See [this post][1]. [1]: http://stackoverflow.com/questions/4748964/android-cancel-asynctask-forcefully – Victor Axelsson Mar 02 '16 at 16:09