2012-02-13 36 views
0

我在使用Live Web服務器的Corona SDK中獲取network.request功能時出現問題。它在我的本地主機上工作正常,但是當我更改代碼以與遠程服務器交談時,應用程序返回一個零值。Lua/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 

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

我已經從我的電腦直接輸入網址到瀏覽器,並得到預期的結果

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

,所以我只能認爲這是一個延遲的問題,該Lua代碼不候爲返回值。 如果有人知道發生了什麼事或者如何解決這個問題,我會非常感激。

問候 格倫

+0

請將您的解決方案,以一個答案,並接受它,這樣的問題確實不保持開放。 – lhf 2012-02-13 10:17:43

回答

0

我最終得到它繞過電暈的下載,並只用LU​​A代碼工作。我還是用在科羅納的JSON解碼器解析JSON的Infor

這裏是櫃面別人的代碼也有類似的問題:

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

如果此線程仍然存在,我對此有疑問。 作爲一種異步類型,當執行到像Galaxy選項卡這樣的設備時,是否會爲您創建問題? 當我使用它,它只是凍結。這是爲什麼? – gadss 2012-06-01 08:28:31