Cocos2d-x3.0 for HTTP request GET, POST

Source: Internet
Author: User

HTTP request implementation

Copy the following code to the new project to see the effect.


HelloWorldScene. h

# Include "cocos2d. h "/* Remember to import the header file */# include" extensions/cocos-ext.h "# include" network/HttpClient. h "USING_NS_CC; USING_NS_CC_EXT; using namespace network; class HelloWorld: public cocos2d: Layer {public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d: Scene * createScene (); // Here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init (); // a selector callback void menuCloseCallback (cocos2d: Ref * pSender ); void onMenuGetTestClicked (Ref * sender); void notify (Ref * sender); // Http Response Callback void onHttpRequestCompleted (cocos2d: network: HttpClient * sender, cocos2d: network :: httpResponse * response); // implement the "static create ()" method manually CREATE_FUNC (HelloWorld );};

HelloWorldScene. cpp

# Include "HelloWorldScene. h "Scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "init" you need to initialize your instancebool HelloWorld: init () {// 1. Super init first if (! Layer: init () {return false;} auto winSize = Director: getInstance ()-> getWinSize (); const int MARGIN = 40; const int SPACE = 70; auto label = Label: createWithTTF ("Http Request Test", "arial. ttf ", 28); label-> setPosition (Point (winSize. width/2, winSize. height-MARGIN); addChild (label); auto menuRequest = Menu: create (); menuRequest-> setPosition (Point: ZERO); addChild (menuRequest ); // Get auto LabelGet = Label: createWithTTF ("Test Get", "arial. ttf ", 44); auto itemGet = MenuItemLabel: create (labelGet, CC_CALLBACK_1 (HelloWorld: onMenuGetTestClicked, this); itemGet-> setPosition (Point (winSize. width/2, winSize. height-MARGIN-SPACE); menuRequest-> addChild (itemGet); auto labelPostBinary = Label: createWithTTF ("Test Post Binary", "arial. ttf ", 44); auto itemPostBinary = MenuItemLabel: cr Eate (labelPostBinary, CC_CALLBACK_1 (HelloWorld: onMenuPostBinaryTestClicked, this); itemPostBinary-> setPosition (Point (winSize. width/2, winSize. height-MARGIN-3 * SPACE); menuRequest-> addChild (itemPostBinary); return true;} void HelloWorld: onMenuGetTestClicked (Ref * sender) {HttpRequest * request = new HttpRequest (); request-> setUrl ("GET request URL"); request-> setRequestType (HttpRequest: Type: GET ); Request-> setResponseCallback (this, httpresponse_selector (HelloWorld: onHttpRequestCompleted); request-> setTag ("GET test1"); HttpClient: getInstance ()-> send (request ); request-> release ();} void HelloWorld: onMenuPostBinaryTestClicked (cocos2d: Ref * sender) {HttpRequest * request = new HttpRequest (); request-> setUrl ("post request URL"); request-> setRequestType (HttpRequest: Type: POST); request-> setResponseCall Back (this, httpresponse_selector (HelloWorld: onHttpRequestCompleted); const char * postData = "parameter params = Value"; request-> setRequestData (postData, strlen (postData )); request-> setTag ("POST test1"); HttpClient: getInstance ()-> send (request); request-> release ();} void HelloWorld: onHttpRequestCompleted (cocos2d:: network: HttpClient * sender, cocos2d: network: HttpResponse * response) {if (! Response) {return;} if (0! = Strlen (response-> getHttpRequest ()-> getTag () {log ("% s completed", response-> getHttpRequest ()-> getTag ());} long statusCode = response-> getResponseCode (); char statusString [64] ={}; sprintf (statusString, "HTTP Status Code: % ld, tag = % s", statusCode, response-> getHttpRequest ()-> getTag (); log ("response code: % ld", statusCode); if (! Response-> isSucceed () {log ("response failed"); log ("error buffer: % s", response-> getErrorBuffer (); return;} std :: vector
 
  
* Buffer = response-> getResponseData (); printf ("Http Test, dump data:"); for (unsigned int I = 0; I <buffer-> size (); I ++) {printf ("% c", (* buffer) [I]);} printf ("\ n ");}
 


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.