http.request [Resolved]

Does http.request support native.setActivityIndicator( true )?Coz I am unable to do that .I have 10 images that needs to be downloaded from the server.So,I am using

for i = 1 ,10 do  
 local path = system.pathForFile( namesForImageinEachStory[i], system.DocumentsDirectory )  
 local myFile = io.open( path, "w+b" )  
native.setActivityIndicator( true )  
 http.request{   
 url = linkForImages..namesForImageinEachStory[i],   
 sink = ltn12.sink.file(myFile),  
 }  
 end  

where namesForImageinEachStory = Table for names of the images.
But the problem is that application is not showing “ActivityIndicator”.Can anybody resolve this issue? [import]uid: 45566 topic_id: 34299 reply_id: 334299[/import]

The setActivityindicator is working for me, and I do use http.request in a couple places in my code.

Keep in mind that http.request is synchronous, so the activity indicator may not get a chance to display before the http call executes (and essentially freezes the app).

Some things to try, to give the activityIndicator a chance to display.

  • Try moving the setActivityIndicator call outside your loop
  • Try moving the setActivityIndicator to another function, ie; the function that calls your code above.
  • Try adding a timer performWithDelay call to your http.request, to delay the http.request 50 or 100 ms in order to allow the setAvtivityIndicator to fire off. [import]uid: 79933 topic_id: 34299 reply_id: 136357[/import]

Thanks for the Help it worked for me. [import]uid: 45566 topic_id: 34299 reply_id: 136424[/import]

@ mpappas : Can you tell me how to check whether the images has been download to the documents directory so that after all download I shall change the scene once all the images has been downloaded. [import]uid: 45566 topic_id: 34299 reply_id: 136449[/import]

You could use a piece of code something like this:

function checkFile(filename) -- Checks Document Directory for file  
  
local path = system.pathForFile( filename, system.DocumentsDirectory)  
 local file = io.open( path, "r" )  
  
 if file then  
 return true  
 end  
  
 print(" -- fail, no pre-existing file.")  
 return false  

However, it sounds like you might want to use network.download instead. It doesn’t block execution (your code can do other things while it waits), and has a built in listener that calls your code back/tells you when each file is done downloading, and also gives you information on failure.

http://docs.coronalabs.com/api/library/network/request.html
[import]uid: 79933 topic_id: 34299 reply_id: 136456[/import]

If you are dealing with only one file, you can do an io.open() call to try and read the file. If the io.open() call fails, its not there, if it’s successful, then its there.

If you need to check a bunch of files, look into the “lfs” library. It gives you more access to the file system, and you can get a list of files in a given folder.

http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/ [import]uid: 199310 topic_id: 34299 reply_id: 136465[/import]

The setActivityindicator is working for me, and I do use http.request in a couple places in my code.

Keep in mind that http.request is synchronous, so the activity indicator may not get a chance to display before the http call executes (and essentially freezes the app).

Some things to try, to give the activityIndicator a chance to display.

  • Try moving the setActivityIndicator call outside your loop
  • Try moving the setActivityIndicator to another function, ie; the function that calls your code above.
  • Try adding a timer performWithDelay call to your http.request, to delay the http.request 50 or 100 ms in order to allow the setAvtivityIndicator to fire off. [import]uid: 79933 topic_id: 34299 reply_id: 136357[/import]

Thanks for the Help it worked for me. [import]uid: 45566 topic_id: 34299 reply_id: 136424[/import]

@ mpappas : Can you tell me how to check whether the images has been download to the documents directory so that after all download I shall change the scene once all the images has been downloaded. [import]uid: 45566 topic_id: 34299 reply_id: 136449[/import]

You could use a piece of code something like this:

function checkFile(filename) -- Checks Document Directory for file  
  
local path = system.pathForFile( filename, system.DocumentsDirectory)  
 local file = io.open( path, "r" )  
  
 if file then  
 return true  
 end  
  
 print(" -- fail, no pre-existing file.")  
 return false  

However, it sounds like you might want to use network.download instead. It doesn’t block execution (your code can do other things while it waits), and has a built in listener that calls your code back/tells you when each file is done downloading, and also gives you information on failure.

http://docs.coronalabs.com/api/library/network/request.html
[import]uid: 79933 topic_id: 34299 reply_id: 136456[/import]

If you are dealing with only one file, you can do an io.open() call to try and read the file. If the io.open() call fails, its not there, if it’s successful, then its there.

If you need to check a bunch of files, look into the “lfs” library. It gives you more access to the file system, and you can get a list of files in a given folder.

http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/ [import]uid: 199310 topic_id: 34299 reply_id: 136465[/import]