【问题标题】:Network Request in Lua/Corona SDKLua/Corona SDK 中的网络请求
【发布时间】:2012-03-04 11:49:01
【问题描述】:

我在将 Corona SDK 中的 network.request 函数与实时网络服务器一起使用时遇到问题。它在我的本地主机上运行良好,但是当我更改代码以与远程服务器通信时,应用程序返回 nil 值。

这是我在 .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

------------------
--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

这就是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;   

我已经从我的电脑直接将 URL 输入到浏览器中并获得了预期的结果

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

所以我只能假设这是一个延迟问题,并且 lua 代码没有等待返回值。 如果有人知道发生了什么或如何解决此问题,我将不胜感激。

问候 格伦

【问题讨论】:

  • 请将您的解决方案移至答案并接受它,这样问题就不会保持开放状态。

标签: android iphone json lua coronasdk


【解决方案1】:

我最终通过绕过 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

【讨论】:

  • 如果这个帖子还活着,我有一个问题。作为一种异步类型,当执行到像 Galaxy 选项卡这样的设备时,这会给您带来问题吗?当我使用它时,它只是冻结。这是为什么呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 2012-05-08
  • 2014-01-28
  • 1970-01-01
  • 2013-03-23
  • 2019-06-15
  • 2012-02-06
相关资源
最近更新 更多