Quick-cocos2d-x tutorial 11: Achieve http Communication, and with the site php docking, can achieve login and other common functions

Source: Internet
Author: User
Tags response code

Mobile games are currently dominated by weak networking, while http logon is a common function. We will implement it now.

Automatically requests http. function MainScene: ctor () at startup ()
Local url = "http: // localhost/request. php"
Local request = network. createHTTPRequest (onRequestFinished, url, "POST ")
Request: addPOSTValue ("KEY", "VALUE ")
-- Start the request. The callback () function is called when the request is complete.
Request: start ()
End
--- Do not add MainScence before onRequestFinished.
Function onRequestFinished (event)
Local OK = (event. name = "completed ")
Local request = event. request

If not OK then
-- Request failed, error code and error message displayed
Print (request: getErrorCode (), request: getErrorMessage ())
Return
End

Local code = request: getResponseStatusCode ()
If code ~ = 200 then
-- The request ends, but no 200 response code is returned
Print (code)
Return
End

-- The request is successful and the content returned by the server is displayed.
Local response = request: getResponseString ()
Print (response)
Endrequest. php implementation code:
$ TmpName = $ _ POST ['key'];
Echo $ tmpName. "OK ";
?>
Normal network output:
VALUEok, then we can add the account and password function (request: addPOSTValue ("KEY", "VALUE") on the mobile app, corresponding to the name and VALUE), and then in php, add the corresponding database query function and return different results.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.