network.download - checking HTTP status?

Is there a way to check the HTTP status (possibly the response code) of a download?

I am having an issue (http://developer.anscamobile.com/forum/2011/12/27/error-checking-xml) using an XML parser because I am retrieving an XML file from the Internet. But if the file is not on the server, network.download grabs the 404 page and saves it as the XML file name I specify.

When the XML parser goes to process this, it errors out. And I can't see a way to catch that error and deal with it.

So as someone else suggested, check to see if the XML file is valid in the first place. I thought the easiest way to do this would be to check during the download to see if the XML was not a 404 erorr, and if it is, don't get it.

Is there a way to do that within Corona? I've done this before in JS using AJAX, but not sure how to do this in Corona.

Would appreciate any pointers. (And I am a Corona/LUA newbie, so I apologize if this is something that I should know, though my Google searching didn't bring up any information either.)

Thanks everyone.

both network.request() and network.download() use a call back listener that gives you the status of the download:

See the documentation for it.

1
2
3
4
5
6
7
8
9
10
local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        else
                print ( "RESPONSE: " .. event.response )
        end
end
 
-- Access Google over SSL:
network.request( "https://encrypted.google.com", "GET", networkListener )

Thanks for the suggestion, robmiracle.

Unfortunately, that is the problem. The 404 error is not causing the event.isError to fire. It does fire when there is no network connection, but not with the 404.

(I should also say that I have only tested this in the simulator, not on a device yet. I can't say that a device will behave differently as I am still very new to developing with Corona.)

I also tried the print_r(event) suggestion and here is the result:
[isError] => false
[name] => "networkRequest"
[response] => "/Users/XXXXXX/Library/Application Support/Corona Simulator/3-7C276F8012F18A27F73EBBD780F499D3/tmp/12312011.xml"

With the network connection disabled, the result is:
[isError] => true
[name] => "networkRequest"
[response] => "This computer’s Internet connection appears to be offline."

I understand that verifying the URLs ahead of time is the best course of action.

I know that the URLs are all valid from a naming perspective, but I can't guarantee that the files will always be there. For example, if a user changes the date on their phone to something in the future for some reason, then the app will try requesting an XML file that won't be there yet. I want to have a good fallback for this.

Thanks to someone else who suggested using pcall() on the XML parsing, I have figured out a way to handle this.

Would still be great to know about catching the 404 error, but at least I do have a way to trap the error I was experiencing.

views:1224 update:2012/1/3 13:02:13
corona forums © 2003-2011