Lua/Corona SDK中的网络请求

我在使用Lua文件中的network.request函数与一个实时的Web服务器在Corona SDK上有问题。该函数对于我的本地主机运作良好,但是一旦我更改代码以与远程服务器通信,应用程序返回空值。

这是我在.lua文件中使用的代码:

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

------------------
--将请求发送到网站以注册用户。
------------------
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

这是PHP服务器代码应以JSON数据返回的内容:

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

如果请求失败,则返回以下代码。

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

我已经直接在我的PC浏览器中输入URL并获得了预期结果

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

因此我只能假定这是一个延迟问题,而Lua代码并未等待返回值。 如果有人知道发生了什么或如何解决此问题,我将非常感激。

问候 Glen

原文链接 https://stackoverflow.com/questions/9254379

点赞
stackoverflow用户241858
stackoverflow用户241858

我最终通过绕过Corona进行下载并只使用LUA代码来使之工作。尽管如此,我仍然使用Corona中的JSON解码器来解析JSON信息。

如果其他人遇到类似问题,以下是代码:

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
2012-02-13 22:00:14