external json and transmission parameters

Hi there, need your help, guys!

I made something like that:

http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/

I have 2 lua files,

One of them - displaying and operating with data.

Second - have all information about json server, urls, functions to get data from server.

So at second file i have same code as in tutorial:

    local function loginCallback(event)

        if ( event.isError ) then

            print( “Network error!”)

        else

            print ( "RESPONSE: " … event.response )

            local data = json.decode(event.response)

            – do with data what you want…

        end

        return true

    end

How can i transfer all data from this function to first lua file?I want to process this data on screen, but i want to split network and data processing

We probably need to know more about the structure of your program to be able to advise accordingly.  Probably one of the easiest is to set up a function in your first lua file that the 2nd lua file can call.  Then we just have to figure out how to get that function’s address to the 2nd file to call.  

Are you using storyboard?  Is 2nd lua a self contained module?

You are absolutely right, i’m using storyboard as first lua script and the second is self contained module.

First lusa script has this code:


local function country_obj(country_image,country_mask,json_name)

local this_country = display.newImage(country_image)

this_country.jsonname = json_name

placeCountry(this_country)

local masked_country = graphics.newMask( country_mask )

this_country:setMask( masked_country )

local function onTouch(event)

    if “ended” == event.phase then

    jsonRequester : GetInfo(nil,“mapinfo.php”,“mapinfo”,“country_index”,event.target.jsonname,nil,nil,nil,nil);

    end

end

this_country:addEventListener(“touch”,onTouch)

return this_country

end

and second script contain:


local jsonFile = function( filename, base,my_var )

– set default base dir if none specified

    if not base then base = system.ResourceDirectory; end

    – create a file path for corona i/o

    local path = system.pathForFile( filename, base );

    – will hold contents of file

    local contents

    – io.open opens a file at path. returns nil if no file found

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

    if file then

        – read all contents of file into a string

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

        io.close( file ); – close the file after using it

    end

    return contents ;

end

local function getmapinfo( event)

    if ( event.isError ) then

        storyboard.errno = 1;

        storyboard.gotoScene( “maintaince” )

        return true

    else

        print ( "RESPONSE: " … event.response );

        local t = json.decode( event.response );

        – Go through the array in a loop

        for key in pairs(t) do

            – Here you can do whatever you like with the values

        –    print(t[key][“index”]);

        –    print(t[key][“country_name_eng”]);

        end

        return event.responce

    end

end

function jsonRequester : getInfo (url,script,auth_param,param1,param1_val,param2,param2_val,param3,param3_val)

    local json_file_by_get

    json_file_by_get = jsonFile( network.request( “https://”…serverip…"/"…script…"?auth="…auth_param…"&"…param1…"="…param1_val, “GET”, getmapinfo));

    end

end

Not sure how to phrase this, but you could attach the functions in the second class to a table, and have the second class return that table.

E.g

mySecondClass.lua

local mySecondClass = {} mySecondClass.jsonFile = function( filename, base,my\_var ) -- set default base dir if none specified if not base then base = system.ResourceDirectory; end -- create a file path for corona i/o local path = system.pathForFile( filename, base ); -- will hold contents of file local contents -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) ; if file then -- read all contents of file into a string contents = file:read( "\*a" ) ; io.close( file ); -- close the file after using it end return contents ; end mySecondClass.getmapinfo = function( event) if ( event.isError ) then storyboard.errno = 1; storyboard.gotoScene( "maintaince" ) return true else print ( "RESPONSE: " .. event.response ); local t = json.decode( event.response ); -- Go through the array in a loop for key in pairs(t) do -- Here you can do whatever you like with the values -- print(t[key]["index"]); -- print(t[key]["country\_name\_eng"]); end return event.responce end end mySecondClass.getInfo = function(url,script,auth\_param,param1,param1\_val,param2,param2\_val,param3,param3\_val) local json\_file\_by\_get json\_file\_by\_get = mySecondClass.jsonFile( network.request( "https://"..serverip.."/"..script.."?auth="..auth\_param.."&"..param1.."="..param1\_val, "GET", mySecondClass.getmapinfo)); end return mySecondClass

Then in first script:

infoClass = require("mySecondClass") local mapInfo = infoClass.getInfo(your params here)

We probably need to know more about the structure of your program to be able to advise accordingly.  Probably one of the easiest is to set up a function in your first lua file that the 2nd lua file can call.  Then we just have to figure out how to get that function’s address to the 2nd file to call.  

Are you using storyboard?  Is 2nd lua a self contained module?

You are absolutely right, i’m using storyboard as first lua script and the second is self contained module.

First lusa script has this code:


local function country_obj(country_image,country_mask,json_name)

local this_country = display.newImage(country_image)

this_country.jsonname = json_name

placeCountry(this_country)

local masked_country = graphics.newMask( country_mask )

this_country:setMask( masked_country )

local function onTouch(event)

    if “ended” == event.phase then

    jsonRequester : GetInfo(nil,“mapinfo.php”,“mapinfo”,“country_index”,event.target.jsonname,nil,nil,nil,nil);

    end

end

this_country:addEventListener(“touch”,onTouch)

return this_country

end

and second script contain:


local jsonFile = function( filename, base,my_var )

– set default base dir if none specified

    if not base then base = system.ResourceDirectory; end

    – create a file path for corona i/o

    local path = system.pathForFile( filename, base );

    – will hold contents of file

    local contents

    – io.open opens a file at path. returns nil if no file found

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

    if file then

        – read all contents of file into a string

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

        io.close( file ); – close the file after using it

    end

    return contents ;

end

local function getmapinfo( event)

    if ( event.isError ) then

        storyboard.errno = 1;

        storyboard.gotoScene( “maintaince” )

        return true

    else

        print ( "RESPONSE: " … event.response );

        local t = json.decode( event.response );

        – Go through the array in a loop

        for key in pairs(t) do

            – Here you can do whatever you like with the values

        –    print(t[key][“index”]);

        –    print(t[key][“country_name_eng”]);

        end

        return event.responce

    end

end

function jsonRequester : getInfo (url,script,auth_param,param1,param1_val,param2,param2_val,param3,param3_val)

    local json_file_by_get

    json_file_by_get = jsonFile( network.request( “https://”…serverip…"/"…script…"?auth="…auth_param…"&"…param1…"="…param1_val, “GET”, getmapinfo));

    end

end

Not sure how to phrase this, but you could attach the functions in the second class to a table, and have the second class return that table.

E.g

mySecondClass.lua

local mySecondClass = {} mySecondClass.jsonFile = function( filename, base,my\_var ) -- set default base dir if none specified if not base then base = system.ResourceDirectory; end -- create a file path for corona i/o local path = system.pathForFile( filename, base ); -- will hold contents of file local contents -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) ; if file then -- read all contents of file into a string contents = file:read( "\*a" ) ; io.close( file ); -- close the file after using it end return contents ; end mySecondClass.getmapinfo = function( event) if ( event.isError ) then storyboard.errno = 1; storyboard.gotoScene( "maintaince" ) return true else print ( "RESPONSE: " .. event.response ); local t = json.decode( event.response ); -- Go through the array in a loop for key in pairs(t) do -- Here you can do whatever you like with the values -- print(t[key]["index"]); -- print(t[key]["country\_name\_eng"]); end return event.responce end end mySecondClass.getInfo = function(url,script,auth\_param,param1,param1\_val,param2,param2\_val,param3,param3\_val) local json\_file\_by\_get json\_file\_by\_get = mySecondClass.jsonFile( network.request( "https://"..serverip.."/"..script.."?auth="..auth\_param.."&"..param1.."="..param1\_val, "GET", mySecondClass.getmapinfo)); end return mySecondClass

Then in first script:

infoClass = require("mySecondClass") local mapInfo = infoClass.getInfo(your params here)