Hello everyone
In my app I am trying to communicate with the server and want to know how to wait for the response from the sever. I post a bit of code to explain:
Corona
local json = require ( "json" ) local myNewData local decodedData local function networkListener( event ) if ( event.isError ) then print( "Network error!" ) else myNewData = event.response print ( "From server: " .. myNewData ) decodedData = ( json.decode( myNewData ) ) end end local function searchMovie( title ) title = title or "" tesTab = {} --possible tables local body = "titleMovie="..title.."&tesTab ="..tesTab local params = {} params.headers = headers params.body = body network.request( "http://mySite//getMovie.php", "POST", networkListener, params ) if(decodedData) then --my code end end searchMovie("Back to the Future")
In server (getMovie.php)
\<?php $host = "hostName"; $username = "user"; $password = "pass"; $database = "my\_DB"; $title = $\_POST["titleMovie"]; $conn = mysql\_connect($host, $username, $password) or die ("Not conn server"); mysql\_select\_db("$database", $conn) or die ("Not select DB"); $interrogation = "SELECT \* FROM Movies WHERE title = '$titolo' "; $resultInterrogation = mysql\_query($interrogation); $json = array(); if(mysql\_num\_rows($resultInterrogation)) { while($row = mysql\_fetch\_row($resultInterrogation)) { $json[] = $row; } } mysql\_close($database); echo json\_encode($json); ?\>
of course, it will never enter into the condition (decode is true) because of the response times.
I have two questions:
1-How can I fix?
2-Is the best way?