Stopping a download.

NJDude

Expert
Licensed User
Longtime User
I have a download routine in a service, it works just fine, but, I'd like to have the option to cancel it, I've tried StopService and done some "notification" and other cancellations under Service_Destroy but it doesn't stop the download.

I must be missing something but I can't figure out what, any help will be appreciated.

Thanks.

NOTE: I don't use HttpUtils.
 
Last edited:

NJDude

Expert
Licensed User
Longtime User
Ok, the problem I'm having is that I can stop the download (it seems) but if I try to start the service again the download part doesn't seem to work, I'm going to write a sample of that routine alone and post it.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Well, I'm stuck.

I isolated the download routine (to post here) but IT WORKS!!!, my code does work too but only once, it has to be my code but I can't figure out where, interesting enough, is basically the same code as the sample, so, I need to take a break :BangHead:
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Yes, I'm using the same TaskID, the reason is because I'm allowing to download 1 file at a time.

It's really frustrating, but I'll try changing the TaskID and see what happens.

EDIT TO ADD: Ok, changing the TaskID seems to work, however, the previous download still continues in "ghost" mode and makes the device crawl to a halt.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've just made a test where I close the output stream and it does stop the download:
B4X:
Sub Process_Globals
   Dim hc As HttpClient
   Dim out As OutputStream
End Sub
Sub Globals
   
End Sub
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      hc.Initialize("hc")
   End If
   Dim req As HttpRequest
   req.InitializeGet("http://xxx.xxx.xx")
   hc.Execute(req, 1)
End Sub
Sub Activity_Click
   out.Close
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   out = File.OpenOutput(File.DirRootExternal, "1.dat", False)
  Log("Getting file asynchronously")
   response.GetAsynchronously("res", out, True, TaskId)
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   
End Sub

Sub res_StreamFinish (Success As Boolean, TaskId As Int)
   Log("StreamFinish: " & Success)
   If Success = False Then Log(LastException.Message)
End Sub
Sub Activity_Pause(UserClosed As Boolean)

End Sub
Sub Activity_Resume

End Sub
You can see that StreamFinish event is raised with an error when you close the output.
Start the program and after you see "Getting file asynchronously" message in the logs click on the activity. You will see that StreamFinish is raised.
You can rotate the device to start a new download.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Ok, this is what I've found.

Moving these lines from Process_Globals to Service_Start and adding the out.Close to the Service_Destroy stops the download.

B4X:
Sub Service_Start

    Dim hc As HttpClient
    Dim req As HttpRequest

    hc.Initialize("hc")

... etc etc

Sub Service_Destroy

... some code here
    Out.Close

End Sub

I'm going to keep testing to make sure this is the case. I have to thank you for that clue (Out.Close).
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Sorry I have to bring back this old thread. I understand that you 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?
 
Upvote 0
Top