network.download question

Hi

I am playing around with network.download, trying to understand how it works. I have the following in a main.lua file:

network.download(

        “http://www.davidpowell.ca/appFolder/stable_of_questions.json”, 

        “GET”,

        networkListener,

        params,

        “stable_of_questions.json”, 

        system.DocumentsDirectory

        )

        

        

                

network.download(

        “http://www.davidpowell.ca/images/me2.jpg”, 

        “GET”,

        networkListener2,

        params,

        “me2.jpg”, 

        system.DocumentsDirectory

        )

When I run this code, the file “me.2” gets downloaded to the Documents directory (I can see it in the Sandbox), but the json file does not. I have no idea why. Can anyone enlighten me? (Yes, the paths are correct)

thanks.

I got that json file downloaded by duplicating it and deleting the old copy. I still don’t understand why that happened though… :huh:

I have another question about network.download;

I can download the file from the server to system.DocumentsDirectory directory, but

I can’t download the file into a directory which is under system.DocumentsDirectory .

local a\_path = system.pathForFile( "", system.DocumentsDirectory ) a\_path = a\_path.."a\_dir\\" network.download( "http://www.sapteknik.com/sounds/muzikler.txt", "GET", networkListener, "file.txt", a\_path)

Still it keeps downloading the file into system.DocumentsDirectory .

How can I solve this problem?

Btw, I am using Windows 7, and I am simulating it here.

Thanks.

@d2gp, in your network listener, did you get any errors?  What was the event.response value?  If the download fails the the event table should have the reason why.  Adding some print statements to see the values there would be helpful to figure out the problem.

system.pathForFile() doesn’t return a string that you can simply append additional strings to.  You need to make sure the folder exists inside of system.DocumentsDirectory (perhaps using LFS – you can search for the blog post about it).  Then do something iike:

  network.download( "http://www.sapteknik.com/sounds/muzikler.txt", "GET", networkListener, "a\_dir/file.txt", system.DocumentsDirectory)  

The example you give works, thanks.

Hi Rob Miracle

event.response outputs the html source code of a 404 error page, which is odd because I can see my json file sitting on the server. I suspect there’s something wacky going on with the server. 

I just ran this in the simulator and it downloaded your file:

local function networkListener(event)     print(event.status) end network.download(     "http://www.sapteknik.com/sounds/muzikler.txt",     "GET",     networkListener,     "file.txt", system.CachesDirectory)

I got a 200 code meaning success and your file was downloaded to my app’s caches’ directory as expected.

Sometimes my app still seems to have a hard time getting that json file on the server: I’ll make a change to the server file data but I don’t see the change in the app. And the post-network.download event.response value results in an error in the terminal window:

Runtime error

    …rs/dpwell/Dropbox/app projects/flashcards19/main.lua:74: attempt to concatenate field ‘response’ (a table value)

stack traceback:

    

And ideas what’s going on?

thanks,

david

event.response will be nil if event.isError is true.  event.isError happens during timeouts or other reasons why your webserver could not be contacted.  You should test to see if event.isError is true and in the else, work with the event.response.

Hi Rob

This is my networkListener, called by network.download. I have never seen “network error, download failed”.  Below that I have pasted part of the terminal output from this function. I’m not sure why I see ‘nil’ twice.

thanks.

local function networkListener( event )

        

        if ( event.isError ) then

                print( “Network error - download failed” )

        elseif ( event.phase == “began” ) then

                print( “Progress Phase: began” )

        elseif ( event.phase == “ended” ) then

                print( “Event phase ended for json download” ) 

                

        end 

         print(event.response)

         

end

Terminal output:

2013-07-08 20:31:22.210 Corona Simulator[23484:f03] Progress Phase: began

2013-07-08 20:31:22.210 Corona Simulator[23484:f03] nil

2013-07-08 20:31:22.212 Corona Simulator[23484:f03] nil

2013-07-08 20:31:22.212 Corona Simulator[23484:f03] Event phase ended for json download

2013-07-08 20:31:22.213 Corona Simulator[23484:f03] table: 0x109d730a0

In that case it appears things worked and your file was successfully downloaded.  The extra nil you got was from a progress event as it was downloading.

If I were writing that, I would do it more like:

local function networkListener( event )         if ( event.isError ) then                 print( "Network error - download failed" )         elseif ( event.phase == "began" ) then                 print( "Progress Phase: began" )         elseif ( event.phase == "ended" ) then                 print( "Event phase ended for json download" )                  -- getting here means you successfully talked to the webserver, but it doesn't mean your transfer worked.                 -- you need to examine event.status and see if the code is a 1XX or 2XX code which is a success and a                 -- 4XX or 5XX is a failure (3XX is a redirect, and you shouldn't get these)                 print(event.status)                 print(event.response)         end  end

thanks.

I called my hosting company and it cleared up a mystery for me. They use something called ‘varnish caching’ which means that even if I update my data file on the server, the server may continue to serve the old version of the file. I couldn’t figure out why my updates would appear sometimes in my app, but sometimes not. Now I know.  

If you want to bust cache, you can usually do something like:

network.download(
    “http://www.sapteknik.com/sounds/muzikler.txt?cb=42”,
    “GET”,
    networkListener,
    “file.txt”, system.CachesDirectory)

Where the ?cb=42 is a query parameter on GET requests.  The cb, I just made up but means “cachebuster” and the 42 would be a random number or the OS time in seconds.  That way the web server technically gets a different “request” and should not be cached.

Though I’ve never heard of “varnish caching” so not sure this will work.

I think it’s this:

http://en.wikipedia.org/wiki/Varnish_(software)

Whatever it was they did on the hosting side, it’s all working now. The updates always appear in my app.

I got that json file downloaded by duplicating it and deleting the old copy. I still don’t understand why that happened though… :huh:

I have another question about network.download;

I can download the file from the server to system.DocumentsDirectory directory, but

I can’t download the file into a directory which is under system.DocumentsDirectory .

local a\_path = system.pathForFile( "", system.DocumentsDirectory ) a\_path = a\_path.."a\_dir\\" network.download( "http://www.sapteknik.com/sounds/muzikler.txt", "GET", networkListener, "file.txt", a\_path)

Still it keeps downloading the file into system.DocumentsDirectory .

How can I solve this problem?

Btw, I am using Windows 7, and I am simulating it here.

Thanks.

@d2gp, in your network listener, did you get any errors?  What was the event.response value?  If the download fails the the event table should have the reason why.  Adding some print statements to see the values there would be helpful to figure out the problem.

system.pathForFile() doesn’t return a string that you can simply append additional strings to.  You need to make sure the folder exists inside of system.DocumentsDirectory (perhaps using LFS – you can search for the blog post about it).  Then do something iike:

  network.download( "http://www.sapteknik.com/sounds/muzikler.txt", "GET", networkListener, "a\_dir/file.txt", system.DocumentsDirectory)  

The example you give works, thanks.

Hi Rob Miracle

event.response outputs the html source code of a 404 error page, which is odd because I can see my json file sitting on the server. I suspect there’s something wacky going on with the server. 

I just ran this in the simulator and it downloaded your file:

local function networkListener(event)     print(event.status) end network.download(     "http://www.sapteknik.com/sounds/muzikler.txt",     "GET",     networkListener,     "file.txt", system.CachesDirectory)

I got a 200 code meaning success and your file was downloaded to my app’s caches’ directory as expected.