withparadox2
withparadox2

Reputation: 422

How to stop a http request in a thread?

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?

Upvotes: 0

Views: 2525

Answers (2)

Terel
Terel

Reputation: 3917

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.

Upvotes: 0

QuokMoon
QuokMoon

Reputation: 4425

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

now if you wanna shutdown your request use this syntax

httpclient.getConnectionManager().shutdown();

Upvotes: 1

Related Questions