简体   繁体   中英

Network Request in Lua/Corona SDK

I am having issues getting the network.request function in Corona SDK working with a live webserver. It works fine on my localhost however the minute i change the code to talk to a remote server the application returns a nil value.

This is the code that i am using in the .lua file

local function networkListener( event )
    if ( event.isError ) then
        print( "Network error!")
    else
        print ( "RESPONSE: " .. event.response )
        local data = json.decode(event.response)
        responseText.text = data.result;
        messageText.text = data.message;
    end
end

------------------
--Send the request to the website to have the user Registered.
------------------
function AddUser (username, email, password, Device_Type, Device_ID)
        --Register Local
        network.request( "http://localhost/MobileApp/Register.php?loginid=" .. mime.b64(username) .. "&email=" .. mime.b64(email) .. "&password=" .. mime.b64(password) .. "&Device_Type=" .. mime.b64(Device_Type) .. "&Device_ID=" .. mime.b64(Device_ID), "GET", networkListener )
end

This is what the php server code should return in the form of json data:

    $result = array();
    $result["result"] = 200;
    $result["message"] = "Sucessfully Registered";
    echo json_encode($result);
    mysql_free_result($dbresult);
    exit;       

Or this if the request failed.

    $result = array();
    $result["result"] = 401;
    $result["message"] = "Please try another Username";
    echo json_encode($result);
    mysql_free_result($dbresult);
    exit;   

I have entered the URL directly into the browser from my PC and get the expected result

   {"result":200,"message":"Sucessfully Registered"}

so i can only assume that this is a latency issue and that the lua code is not waiting for a return value. If anyone knows what is going on or how to resolve this issue i would be most appreciative.

Regards Glen

I ended up getting it working by bypassing Corona for the downloading and just using LUA code. I still use the json decoder in Corona to parse the json infor

Here is the code incase someone else has a similar issue:

local http = require("socket.http")
local json = require("json")


local URL = "http://localhost/MobileApp/Register.php?loginid=" .. mime.b64(username) .. "&email=" .. mime.b64(email) .. "&password=" .. mime.b64(password) .. "&Device_Type=" .. mime.b64(Device_Type) .. "&Device_ID=" .. mime.b64(Device_ID);
local response = http.request(URL)

if response == nil then
    print("No Dice")
else
    local data = json.decode(response)
    print(data.result);
    print(data.message);    
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM