JSON data from the Internet error

Hello everyone. I use a sample design in my login function:

local jsonFile = function( filename, base )

  print (filename)

  if not base then base = system.ResourceDirectory end

  local path = system.pathForFile( filename, base )

  local contents

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

  if file then

    contents = file:read( “*a” )  – read all contents of file into a string

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

  end

  return contents

end

function networkListenerLogin( event )

  if ( event.isError ) then 

    print( “Network error!”)

  else print ( “RESPONSE” )

    estatus = event.status

    online  = true

    if estatus == 200  then

      t = json.decode( event.response )

      for key in pairs(t) do    

        print (key,t[key])  

      end

   end

 end

end

function getLogin() 

      params.body = “username=”…cnf_user…"&password="…cnf_pass

      json_file_by_get = jsonFile( network.request( "https://api.myweb.com/v1/login",

      “POST”, networkListenerLogin,params ) )

end

In simulator is everything OK, but during a test on phone (I tried 2 different phones) is reported

error “bad argument #1 to ''open” (string expected, got nil)" and filename in local jsonFile is “false”.

In build_setting i have 

“android.permission.INTERNET”,

“android.permission.WRITE_EXTERNAL_STORAGE”,

“android.permission.ACCESS_NETWORK_STATE”,

“android.permission.ACCESS_WIFI_STATE”

I do not know what is wrong.

Thanks for some advice…

json\_file\_by\_get = jsonFile( network.request( "https://api.myweb.com/v1/login", "POST", networkListenerLogin,params ) )

This won’t work. network.request does not return anything. You only get the network response in your networkListenerLogin function as event.response.

You need to process your JSON inside your networkListenerLogin function.

Rob

Hi Rob. I use this code : http://www.e-nature.ch/tech/corona-sdk-get-json-data-from-the-internet/

Can you please write how it is to be done?  I need to function local jsonFile ?

= only request ? network.request( "https://api.myweb.com/v1/login",“POST”, networkListenerLogin,params ) )  ??

Thank You

First, please post code using code formatting. The easiest way is to click the blue <> button in the edit tools bar.

Now for your code, I don’t know who wrote that but it’s really old (2011) and simply won’t work. I’m really limited in how much I can help you because I don’t know what parameters https://api.myweb.com/v1/login takes. I don’t know what it returns. Is this a server script you are in control of? If not is there documentation on it? But you have to know what that script accepts and what it’s going to send back out.

The following assumes it’s going to return some JSON data to you and that you have the parameters right.

local function networkListenerLogin( event ) if ( event.isError ) then print( "Network error!") else print ( "RESPONSE" ) estatus = event.status online = true if estatus == 200 then local t = json.decode( event.response ) --\*\*\*\*\*\*\*\* LOOK AT THIS BLOCK OF CODE \*\*\*\*\*\*\*\*-- for key in pairs(t) do print (key,t[key]) end end end end local function getLogin() local params = {} params.body = "username="..cnf\_user.."&password="..cnf\_pass network.request( "https://api.myweb.com/v1/login", "POST", networkListenerLogin, params ) ) end

The code above simply dumps whatever data your script returns assuming you got a status 200 code back from the server. You need to replace that for loop that’s simply printing out the response data with whatever code you need to handle the login. I don’t know whats being returned. At some point since this is a login, I would assume there would be some thing to indicate that the login was successful. Then you need to call whatever code in your app is needed to process the successful login.

One final point, it’s standard in the Internet world when calling web scripts to URL encode the data. Since you are doing POST, you may not need to since it’s not part of the URL. But if you did a GET request (and many login API endpoints are…), you would need to URL encode the values you are passing. There are a bunch of simple urlencode() functions that can be found with a Google for lua urlencode function.

Rob

Thank you Rob, now it works fine !

json\_file\_by\_get = jsonFile( network.request( "https://api.myweb.com/v1/login", "POST", networkListenerLogin,params ) )

This won’t work. network.request does not return anything. You only get the network response in your networkListenerLogin function as event.response.

You need to process your JSON inside your networkListenerLogin function.

Rob

Hi Rob. I use this code : http://www.e-nature.ch/tech/corona-sdk-get-json-data-from-the-internet/

Can you please write how it is to be done?  I need to function local jsonFile ?

= only request ? network.request( "https://api.myweb.com/v1/login",“POST”, networkListenerLogin,params ) )  ??

Thank You

First, please post code using code formatting. The easiest way is to click the blue <> button in the edit tools bar.

Now for your code, I don’t know who wrote that but it’s really old (2011) and simply won’t work. I’m really limited in how much I can help you because I don’t know what parameters https://api.myweb.com/v1/login takes. I don’t know what it returns. Is this a server script you are in control of? If not is there documentation on it? But you have to know what that script accepts and what it’s going to send back out.

The following assumes it’s going to return some JSON data to you and that you have the parameters right.

local function networkListenerLogin( event ) if ( event.isError ) then print( "Network error!") else print ( "RESPONSE" ) estatus = event.status online = true if estatus == 200 then local t = json.decode( event.response ) --\*\*\*\*\*\*\*\* LOOK AT THIS BLOCK OF CODE \*\*\*\*\*\*\*\*-- for key in pairs(t) do print (key,t[key]) end end end end local function getLogin() local params = {} params.body = "username="..cnf\_user.."&password="..cnf\_pass network.request( "https://api.myweb.com/v1/login", "POST", networkListenerLogin, params ) ) end

The code above simply dumps whatever data your script returns assuming you got a status 200 code back from the server. You need to replace that for loop that’s simply printing out the response data with whatever code you need to handle the login. I don’t know whats being returned. At some point since this is a login, I would assume there would be some thing to indicate that the login was successful. Then you need to call whatever code in your app is needed to process the successful login.

One final point, it’s standard in the Internet world when calling web scripts to URL encode the data. Since you are doing POST, you may not need to since it’s not part of the URL. But if you did a GET request (and many login API endpoints are…), you would need to URL encode the values you are passing. There are a bunch of simple urlencode() functions that can be found with a Google for lua urlencode function.

Rob

Thank you Rob, now it works fine !