Android Question How to cancel an Http request?

Inman

Well-Known Member
Licensed User
Longtime User
I understand that when using HttpClient, I can close the outputstream to cancel the download, but what if I am NOT downloading anything but simply checking if the http link works? I will explain.

I have an app that displays posters from the web. I get related posters by executing a Google Image Search. Then I use Picasso library to load the image into an ImageView. But since Picasso doesn't raise an error if the image url doesn't exist, I check the status of image url manually before pushing it to Picasso. I do this by giving an HttpClient request and checking whether it reaches ResponseSuccess or ResponseError. If it reaches ResponseSuccess, I load the url using Picasso and so I am not using Response.GetAsynchronously here.

Now sometimes the image url might take some time even to simply get a response. Now if a user closes the Activity while a request is in progress, it doesn't get cancelled. Instead, it gets resumed the next time the Activity loads and it will load the incorrect image (from the previous request, not the current one). So ideally I would like to cancel this Http Request in Activity_Pause if UserClosed=True.

How can I do that?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…