quick-cocos2d-x教程11:實現http通訊,並與網站php對接,可實現登入等常見功能

來源:互聯網
上載者:User

標籤:cocos2d   lua   quick-cocos2d-x   手機遊戲   cocos2d-x教程   

手機遊戲目前是弱連網居多,http登入是常用功能。我們現在就來實現。

  • 在啟動時候,自動請求http.
  • function MainScene:ctor()
        local url = "http://localhost/request.php"
        local request = network.createHTTPRequest(onRequestFinished, url, "POST")
        request:addPOSTValue("KEY", "VALUE")
        -- 開始請求。當請求完成時會調用 callback() 函數
        request:start()
    end

  • ---注意onRequestFinished前面,不要加MainScence
    function onRequestFinished(event)
        local ok = (event.name == "completed")
        local request = event.request
     
        if not ok then
            -- 請求失敗,顯示錯誤碼和錯誤訊息
            print(request:getErrorCode(), request:getErrorMessage())
            return
        end
     
        local code = request:getResponseStatusCode()
        if code ~= 200 then
            -- 請求結束,但沒有返回 200 響應代碼
            print(code)
            return
        end
     
        -- 請求成功,顯示服務端返回的內容
        local response = request:getResponseString()
        print(response)
    end
  • request.php實現代碼:
    <?php
    $tmpName=$_POST[‘KEY‘];
    echo $tmpName."ok";
    ?>
  • 正常連網輸出:
    VALUEok
  • 然後我們在手機應用上可以加輸入帳號密碼功能( request:addPOSTValue("KEY", "VALUE"),就對應名字和數值),然後在php中,加入對應的查詢資料庫功能,返回不同的結果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.