Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

roUrlTransfer.AsyncGetToFile not working

Hi,

I am working on an application in which i need to fetch some images from internet. I want to do it asynchronusly as not all the images are used at the same time and i do not want my application to halt for some time. But the problem is roUrlTransfer.AsyncGetToFile is not creating file in tmp directory. I have add my async function in urlUtils.brs and here is the code:

It does not work when i do this:

function http_async_get_to_file(filename as String)
m.Http.EnableFreshConnection(true) 'Don't reuse existing connections
res = m.Http.AsyncGetToFile(filename)
end function


but it does work when i do this:

function http_async_get_to_file(filename as String)
m.Http.EnableFreshConnection(true) 'Don't reuse existing connections
res = m.Http.AsyncGetToFile(filename)
if (res)
event = wait(0, m.Http.GetPort())
if type(event) = "roUrlEvent"

else if event = invalid
m.Http.AsyncCancel()
return false
endif
endif

return true
end function


and i am calling this function like this:


for i = 1 to m.adImageCount - 1
asyncGetImage(m.content.adImageUrls.GetEntry(i))
end for
function asyncGetImage(url As string)
fileName = url
while fileName.Instr("/") <> -1
fileName = fileName.Mid(fileName.Instr("/") + 1)
end while
http = NewHttp(url)
print http.AsyncGetToFile("tmp:/" + fileName)
end function


So can you please guide why it is not working in completely async way (1st implementation)?

I am using the images after 20 seconds so this cannot be the case that images could not be downloaded as in 2nd implementation 1 image takes only 1 second or 2.
0 Kudos
3 REPLIES 3
TheEndless
Channel Surfer

Re: roUrlTransfer.AsyncGetToFile not working

The problem is that your "http" variable is going out of scope as soon as your asyncGetImage function returns, so it's getting disposed before it has a chance to complete the transfer. You could try something like this, instead:

imageTransfers = []
for i = 1 to m.adImageCount - 1
imageTransfers.Push(asyncGetImage(m.content.adImageUrls.GetEntry(i)))
end for
function asyncGetImage(url As string)
fileName = url
while fileName.Instr("/") <> -1
fileName = fileName.Mid(fileName.Instr("/") + 1)
end while
http = NewHttp(url)
print http.AsyncGetToFile("tmp:/" + fileName)
return http
end function

That will keep the url transfers in scope via the imageTransfers array, which you can clean up after the images have been downloaded.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos

Re: roUrlTransfer.AsyncGetToFile not working

Thank you very much TheEndless! It solved the problem. I never thought it could be an issue regarding to scope of objects....
0 Kudos
phoang
Visitor

Re: roUrlTransfer.AsyncGetToFile not working

Is there a complete example of this because I'm having trouble getting the actual image so I can create a bitmap object from it.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.