How to read a page from a website.

Hi,

if i want to implement an app that reads data from a website, is the code in Corona SDK/Lua the same for all devices, Android, iOS and Windows?
 

I tried running the code below in the simulator and i got the error

Attempt to index global ‘http’ (a nil value)

Does anyone have a template/sample code that i could use to read data from a website? Can be just a simple html file.

Thanks!

ttee

display.setStatusBar(display.HiddenStatusBar)

    local path = system.pathForFile( “question.txt”, system.CachesDirectory)

    local myFile = io.open( path, “w” ) 

    

    http.request{ 

        url = “http://web.textfiles.com/computers/386.txt”,  sink = ltn12.sink.file(myFile),

    }

    print(tostring(myFile))

    local contents = “”

    local file = io.open( path, “r” )

    if file then

        – read all contents of file into a string

        local contents = file:read( “*a” )

        io.close( file )

        print(contents)

    else

        print(“file not found”)

    end

The reason you’re getting that error is because there’s no “http” library automatically loaded in your project.  I think you can load it by including [lua]http = require(“socket.http”)[/lua] at the top of your project.

But the disadvantage of the socket library is that it’s blocking, which means your entire application will halt until the download operation completes.  That doesn’t make for a very good user experience.  A better alternative is to use the asynchronous networking library that’s built into Corona.  You can use network/request() to get a webpage or make any network request.  Check out the API here: http://docs.coronalabs.com/api/library/network/request.html.

  • Andrew

Thanks Andrew.

May I check with you what this piece of code does?

sink = ltn12.sink.file(myFile)

Thanks. I copied this http code from the forum.

For network.request, do i also require something?

I rarely use the socket library, so I actually don’t know what that piece of code does.  But you can read the documentation for LuaSocket here and try to figure it out: http://w3.impa.br/~diego/software/luasocket/ltn12.html.

For network.request, you don’t need to have a “require” statement in your code, as the library is automatically available to all Corona projects.

  • Andrew

Hi,

Thanks for the kind responses above.

I have tried the code below. It runs. But i cant seem to figure out where the output file question.jpg is saved. I tried to search in the Mac but couldnt find. Pls could you tell me how to figure out where the output is?

What if i want to display the output in the mobile screen instead? what code should i add?

Thanks

ttee

display.setStatusBar(display.HiddenStatusBar)

    local path = system.pathForFile( “question.jpg”, system.CachesDirectory)

    local myFile = io.open( path, “w” ) 

    

local function networkListener( event )

        if ( event.isError ) then

                print( “Network error!”)

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

                if event.bytesEstimated <= 0 then

                        print( “Download starting, size unknown” )

                else

                        print( "Download starting, estimated size: " … event.bytesEstimated )

                end

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

                if event.bytesEstimated <= 0 then

                        print( "Download progress: " … event.bytesTransferred )

                else

                        print( "Download progress: " … event.bytesTransferred … " of estimated: " … event.bytesEstimated )

                end

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

                print( "Download complete, total bytes transferred: " … event.bytesTransferred )

        end

end

local params = {}

– This tells network.request() that we want the ‘began’ and ‘progress’ events…

params.progress = “download”

– This tells network.request() that we want the output to go to a file…

params.response = {

        filename = “question.jpg”,

        baseDirectory = system.DocumentsDirectory

        }

network.request( “https://www.coronalabs.com/image.jpg”, “GET”, networkListener,  params )

    print(tostring(myFile))

    local contents = “”

    local file = io.open( path, “r” )

    if file then

        – read all contents of file into a string

        local contents = file:read( “*a” )

        io.close( file )

        print(contents)

    else

        print(“file not found”)

    end

In the Corona Simulator, click File|Show Project Sandbox.  That’ll take you to the folder on your computer with the Documents directory.

If you want to display an image, you should use display.newImage() or display.newImageRect().  Since you downloaded the file to the Documents directory, when you use display.newImage() or display.newImageRect(), you’ll want to use the baseDirectory parameter so that it looks in the Documents directory (because by default, it looks in the Resource directory).

You could also use display.loadRemoteImage() (http://docs.coronalabs.com/api/library/display/loadRemoteImage.html), which is a shorthand way of downloading an image and then displaying it.

Also, one other tip.  When you paste code into a forum post, it’s a good idea to enclose your code in lua tags so that it’s formatted better and easier to read.  Below the box where you type in your post, there should be a button called Post Formatting Tips.  Click on that, and it’ll show you how to enclose your code in lua tags.

  • Andrew

Ok, thanks Andrew for the great help and advice!

Hi All,

I am trying to read a file after i GET it from a website.

When the file is read, look for a line that matches some words, then save that line into a variable.
May i know how i can do this?
I have tried the code below but i am not able to see the line i want being printed in the terminal.

Do you have a sample code that i could use? Hopefully with code that prints the content of the line to the mobile screen not the terminal screen.

Thanks,

ttee

  1. display.setStatusBar(display.HiddenStatusBar)
  2.  
  3.     local path = system.pathForFile( “question.txt”, system.CachesDirectory)
  4.     local myFile = io.open( path, “w” ) 
  5.     
  6. local function networkListener( event )
  7.         if ( event.isError ) then
  8.                 print( “Network error!”)
  9.         elseif ( event.phase == “began” ) then
  10.                 if event.bytesEstimated <= 0 then
  11.                         print( “Download starting, size unknown” )
  12.                 else
  13.                         print( "Download starting, estimated size: " … event.bytesEstimated )
  14.                 end
  15.         elseif ( event.phase == “progress” ) then
  16.                 if event.bytesEstimated <= 0 then
  17.                         print( "Download progress: " … event.bytesTransferred )
  18.                 else
  19.                         print( "Download progress: " … event.bytesTransferred … " of estimated: " … event.bytesEstimated )
  20.                 end
  21.         elseif ( event.phase == “ended” ) then
  22.                 print( "Download complete, total bytes transferred: " … event.bytesTransferred )
  23.         end
  24. end
  25.  
  26. local params = {}
  27.  
  28. – This tells network.request() that we want the ‘began’ and ‘progress’ events…
  29. params.progress = “download”
  30.  
  31. – This tells network.request() that we want the output to go to a file…
  32. params.response = {
  33.         filename = “question.txt”,
  34.         baseDirectory = system.DocumentsDirectory
  35.         }
  36.  
  37. network.request( “http://lua-users.org/wiki/StringRecipes”, “GET”, networkListener,  params )
  38.  
  39.     print(tostring(myFile))
  40.         print(“hello”)
  41.     local contents = “”
  42.     local file = io.open( path, “r” )
  43.     if (file) then
  44.         – read all contents of file into a string
  45.         local contents = file:read( “*a” )
  46.         
  47.         for line in file:lines() do
  48. if line:match “Match Email addresses” then
  49. print( line )
  50. end
  51. end
  52.         io.close( file )
  53.         print(contents)
  54.  
  55.     else
  56.         print(“file not found”)
  57.     end
  58.         print(path)
  59.  
  60.         print(contents)
  61.  
  62.  

The reason you’re getting that error is because there’s no “http” library automatically loaded in your project.  I think you can load it by including [lua]http = require(“socket.http”)[/lua] at the top of your project.

But the disadvantage of the socket library is that it’s blocking, which means your entire application will halt until the download operation completes.  That doesn’t make for a very good user experience.  A better alternative is to use the asynchronous networking library that’s built into Corona.  You can use network/request() to get a webpage or make any network request.  Check out the API here: http://docs.coronalabs.com/api/library/network/request.html.

  • Andrew

Thanks Andrew.

May I check with you what this piece of code does?

sink = ltn12.sink.file(myFile)

Thanks. I copied this http code from the forum.

For network.request, do i also require something?

I rarely use the socket library, so I actually don’t know what that piece of code does.  But you can read the documentation for LuaSocket here and try to figure it out: http://w3.impa.br/~diego/software/luasocket/ltn12.html.

For network.request, you don’t need to have a “require” statement in your code, as the library is automatically available to all Corona projects.

  • Andrew

Hi,

Thanks for the kind responses above.

I have tried the code below. It runs. But i cant seem to figure out where the output file question.jpg is saved. I tried to search in the Mac but couldnt find. Pls could you tell me how to figure out where the output is?

What if i want to display the output in the mobile screen instead? what code should i add?

Thanks

ttee

display.setStatusBar(display.HiddenStatusBar)

    local path = system.pathForFile( “question.jpg”, system.CachesDirectory)

    local myFile = io.open( path, “w” ) 

    

local function networkListener( event )

        if ( event.isError ) then

                print( “Network error!”)

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

                if event.bytesEstimated <= 0 then

                        print( “Download starting, size unknown” )

                else

                        print( "Download starting, estimated size: " … event.bytesEstimated )

                end

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

                if event.bytesEstimated <= 0 then

                        print( "Download progress: " … event.bytesTransferred )

                else

                        print( "Download progress: " … event.bytesTransferred … " of estimated: " … event.bytesEstimated )

                end

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

                print( "Download complete, total bytes transferred: " … event.bytesTransferred )

        end

end

local params = {}

– This tells network.request() that we want the ‘began’ and ‘progress’ events…

params.progress = “download”

– This tells network.request() that we want the output to go to a file…

params.response = {

        filename = “question.jpg”,

        baseDirectory = system.DocumentsDirectory

        }

network.request( “https://www.coronalabs.com/image.jpg”, “GET”, networkListener,  params )

    print(tostring(myFile))

    local contents = “”

    local file = io.open( path, “r” )

    if file then

        – read all contents of file into a string

        local contents = file:read( “*a” )

        io.close( file )

        print(contents)

    else

        print(“file not found”)

    end

In the Corona Simulator, click File|Show Project Sandbox.  That’ll take you to the folder on your computer with the Documents directory.

If you want to display an image, you should use display.newImage() or display.newImageRect().  Since you downloaded the file to the Documents directory, when you use display.newImage() or display.newImageRect(), you’ll want to use the baseDirectory parameter so that it looks in the Documents directory (because by default, it looks in the Resource directory).

You could also use display.loadRemoteImage() (http://docs.coronalabs.com/api/library/display/loadRemoteImage.html), which is a shorthand way of downloading an image and then displaying it.

Also, one other tip.  When you paste code into a forum post, it’s a good idea to enclose your code in lua tags so that it’s formatted better and easier to read.  Below the box where you type in your post, there should be a button called Post Formatting Tips.  Click on that, and it’ll show you how to enclose your code in lua tags.

  • Andrew

Ok, thanks Andrew for the great help and advice!

Hi All,

I am trying to read a file after i GET it from a website.

When the file is read, look for a line that matches some words, then save that line into a variable.
May i know how i can do this?
I have tried the code below but i am not able to see the line i want being printed in the terminal.

Do you have a sample code that i could use? Hopefully with code that prints the content of the line to the mobile screen not the terminal screen.

Thanks,

ttee

  1. display.setStatusBar(display.HiddenStatusBar)
  2.  
  3.     local path = system.pathForFile( “question.txt”, system.CachesDirectory)
  4.     local myFile = io.open( path, “w” ) 
  5.     
  6. local function networkListener( event )
  7.         if ( event.isError ) then
  8.                 print( “Network error!”)
  9.         elseif ( event.phase == “began” ) then
  10.                 if event.bytesEstimated <= 0 then
  11.                         print( “Download starting, size unknown” )
  12.                 else
  13.                         print( "Download starting, estimated size: " … event.bytesEstimated )
  14.                 end
  15.         elseif ( event.phase == “progress” ) then
  16.                 if event.bytesEstimated <= 0 then
  17.                         print( "Download progress: " … event.bytesTransferred )
  18.                 else
  19.                         print( "Download progress: " … event.bytesTransferred … " of estimated: " … event.bytesEstimated )
  20.                 end
  21.         elseif ( event.phase == “ended” ) then
  22.                 print( "Download complete, total bytes transferred: " … event.bytesTransferred )
  23.         end
  24. end
  25.  
  26. local params = {}
  27.  
  28. – This tells network.request() that we want the ‘began’ and ‘progress’ events…
  29. params.progress = “download”
  30.  
  31. – This tells network.request() that we want the output to go to a file…
  32. params.response = {
  33.         filename = “question.txt”,
  34.         baseDirectory = system.DocumentsDirectory
  35.         }
  36.  
  37. network.request( “http://lua-users.org/wiki/StringRecipes”, “GET”, networkListener,  params )
  38.  
  39.     print(tostring(myFile))
  40.         print(“hello”)
  41.     local contents = “”
  42.     local file = io.open( path, “r” )
  43.     if (file) then
  44.         – read all contents of file into a string
  45.         local contents = file:read( “*a” )
  46.         
  47.         for line in file:lines() do
  48. if line:match “Match Email addresses” then
  49. print( line )
  50. end
  51. end
  52.         io.close( file )
  53.         print(contents)
  54.  
  55.     else
  56.         print(“file not found”)
  57.     end
  58.         print(path)
  59.  
  60.         print(contents)
  61.  
  62.