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. [import]uid: 17827 topic_id: 19742 reply_id: 319742[/import]

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

See the documentation for it.

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 )  

If you get a 404, the event.isError should fire. Look in the community code for the print_r function. Its great for printing table contents. You can print the event table by adding:

print_r(event) before the “if” in the networkListener function and see everything that you’re getting back from the server.

I’ve found its best to get a copy of the URL you’re trying to request and test it in a browser by pasting it into the location bar. Once you know you have a good, functional URL, then your in-app work becomes much easier.
[import]uid: 19626 topic_id: 19742 reply_id: 76431[/import]

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. [import]uid: 17827 topic_id: 19742 reply_id: 76830[/import]

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. [import]uid: 17827 topic_id: 19742 reply_id: 76836[/import]