Reading downloaded text files

Hi all,

Right now I have it downloading a default .txt file with some text in it, that I would like to put into a native alert to let players know of my new games.

The .txt file is downloading properly, however, I was confused as to how to make it be read and put into the alert popup dialogue box.

For example, I have it downloading a title file to change the text in the alert’s title.   However, I do not know how to make it actually place in the downloaded text into the title area.

Here’s my code:

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( "Displaying response file" ) local path = system.pathForFile( "title.txt", system.TemporaryDirectory ) local file = io.open( path, "r" ) for line in file:lines() do print( line ) end io.close( file ) file = nil end end local params = {} params.progress = true network.download( "http://www.my\_url.com/title.txt", "GET", networkListener, params, "title.txt", system.TemporaryDirectory ) local function onComplete( event ) if event.action == "clicked" then local i = event.index if i == 1 then -- Do nothing; dialog will simply dismiss elseif i == 2 then -- Open URL if "Learn More" (second button) was clicked system.openURL( "MY APP URL THAT WAS DOWNLOADED WILL GO HERE" ) end end end 

local alert = native.showAlert( "MY DOWNLOADED TITLE HERE", "MY DOWNLOADED DESCRIPTION HERE", { "Later", "Learn More" }, onComplete )

I would also like to make it so that I can have three files download, 1 for the title, 1 for the description, and 1 for the link that is opened when pressing the button. Is the network download repeatable for multiple files?  

Thanks.

I think you’re over complicating this.  But you do need to think about how your text is formatted. It sounds like you need one file with three lines in it. If you can guarantee that is the format you will use, then you can do:

-- somewhere near the top of the app: local lines = {} -- inside your network.request call back local file = io.open( path, "r" ) -- inside your onComplete function system.openURL( lines[3] ) for line in file:lines() do     lines[#lines + 1] = line end io.close( file ) local alert = native.showAlert( lines[1],lines[2], { "Later", "Learn More" }, onComplete )

Keep in mind, the way Lua compiles things, your onComplete() function needs to be defined before you use it, so you will need to move it above the network.request’s listener function.

Rob

Hi,

I tried it out, however, have had some issues getting it to work.

Here’s the code:

local lines = {} local function alertListener( event ) if ( event.isError ) then isError = 1 elseif ( event.phase == "began" ) then elseif ( event.phase == "ended" ) then print( "Displaying response file" ) local path = system.pathForFile( "alert.txt", system.TemporaryDirectory ) local file = io.open( path, "r" ) end end network.download( "my link is here", "GET", titleListener, "alert.txt", system.TemporaryDirectory ) local function onComplete( event ) if event.action == "clicked" then local i = event.index if i == 1 then -- Do nothing; dialog will simply dismiss elseif i == 2 then -- Open URL if "Learn More" (second button) was clicked system.openURL( lines[3] ) end end end function showAlert( event ) for line in file:lines() do lines[#lines + 1] = line end io.close( file ) local alert = native.showAlert( lines[1],lines[2], { "Later", "Learn More" }, onComplete ) end

Here’s what the txt file is like:

New Game! My game descriptions will go here... http://itunes.apple.com/us/app/.../...

When opening the URL, I get an error :

system.openURL() expects a string URL as argument #1 (got nil)

I also get an error when trying to show the lines[1] and lines[2], where it says file is a nil value.

attempt to index global ‘file’ (a nil value)

If you have any ideas as to what could be causing this, that’d be great.

Thanks for the help :).

Hmm some of my code got jumbled up… Any way try this:

local lines = {} local function onComplete( event ) if event.action == "clicked" then local i = event.index if i == 1 then -- Do nothing; dialog will simply dismiss elseif i == 2 then -- Open URL if "Learn More" (second button) was clicked system.openURL( lines[3] ) end end end 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( "Displaying response file" ) local path = system.pathForFile( "title.txt", system.TemporaryDirectory ) local file = io.open( path, "r" ) for line in file:lines() do lines[#lines + 1] = line end io.close( file ) file = nil local alert = native.showAlert( lines[1],lines[2], { "Later", "Learn More" }, onComplete ) end end local params = {} params.progress = true network.download( "http://www.my\_url.com/title.txt", "GET", networkListener, params, "title.txt", system.TemporaryDirectory )

Rob

Thank you so much! It now works flawlessly :).

I really appreciate the help.

I think you’re over complicating this.  But you do need to think about how your text is formatted. It sounds like you need one file with three lines in it. If you can guarantee that is the format you will use, then you can do:

-- somewhere near the top of the app: local lines = {} -- inside your network.request call back local file = io.open( path, "r" ) -- inside your onComplete function system.openURL( lines[3] ) for line in file:lines() do     lines[#lines + 1] = line end io.close( file ) local alert = native.showAlert( lines[1],lines[2], { "Later", "Learn More" }, onComplete )

Keep in mind, the way Lua compiles things, your onComplete() function needs to be defined before you use it, so you will need to move it above the network.request’s listener function.

Rob

Hi,

I tried it out, however, have had some issues getting it to work.

Here’s the code:

local lines = {} local function alertListener( event ) if ( event.isError ) then isError = 1 elseif ( event.phase == "began" ) then elseif ( event.phase == "ended" ) then print( "Displaying response file" ) local path = system.pathForFile( "alert.txt", system.TemporaryDirectory ) local file = io.open( path, "r" ) end end network.download( "my link is here", "GET", titleListener, "alert.txt", system.TemporaryDirectory ) local function onComplete( event ) if event.action == "clicked" then local i = event.index if i == 1 then -- Do nothing; dialog will simply dismiss elseif i == 2 then -- Open URL if "Learn More" (second button) was clicked system.openURL( lines[3] ) end end end function showAlert( event ) for line in file:lines() do lines[#lines + 1] = line end io.close( file ) local alert = native.showAlert( lines[1],lines[2], { "Later", "Learn More" }, onComplete ) end

Here’s what the txt file is like:

New Game! My game descriptions will go here... http://itunes.apple.com/us/app/.../...

When opening the URL, I get an error :

system.openURL() expects a string URL as argument #1 (got nil)

I also get an error when trying to show the lines[1] and lines[2], where it says file is a nil value.

attempt to index global ‘file’ (a nil value)

If you have any ideas as to what could be causing this, that’d be great.

Thanks for the help :).

Hmm some of my code got jumbled up… Any way try this:

local lines = {} local function onComplete( event ) if event.action == "clicked" then local i = event.index if i == 1 then -- Do nothing; dialog will simply dismiss elseif i == 2 then -- Open URL if "Learn More" (second button) was clicked system.openURL( lines[3] ) end end end 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( "Displaying response file" ) local path = system.pathForFile( "title.txt", system.TemporaryDirectory ) local file = io.open( path, "r" ) for line in file:lines() do lines[#lines + 1] = line end io.close( file ) file = nil local alert = native.showAlert( lines[1],lines[2], { "Later", "Learn More" }, onComplete ) end end local params = {} params.progress = true network.download( "http://www.my\_url.com/title.txt", "GET", networkListener, params, "title.txt", system.TemporaryDirectory )

Rob

Thank you so much! It now works flawlessly :).

I really appreciate the help.