Cocos2d-x_HTTP网络编程

Cocos2d-x_HTTP网络编程,第1张

概述#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class HelloWorld : public cocos2d::CCLayer{public: virt
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class HelloWorld : public cocos2d::cclayer{public:    virtual bool init();      static cocos2d::CCScene* scene();    voID menuCloseCallback(CCObject* pSender);	voID getServerInfo();	voID onhttpRequestServerInfoCompleted(CChttpClIEnt *sender,CChttpResponse *response);    CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__

#include "HelloWorldScene.h"CCScene* HelloWorld::scene(){    CCScene *scene = CCScene::create();    HelloWorld *layer = HelloWorld::create();    scene->addChild(layer);    return scene;}bool HelloWorld::init(){    if ( !cclayer::init() )    {        return false;    }        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();    CcmenuItemImage *pCloseItem = CcmenuItemImage::create(                                        "img/Closenormal.png","img/CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));    	pCloseItem->setposition(ccp(origin.x + visibleSize.wIDth - pCloseItem->getContentSize().wIDth/2,origin.y + pCloseItem->getContentSize().height/2));    Ccmenu* pMenu = Ccmenu::create(pCloseItem,NulL);    pMenu->setposition(CCPointZero);    this->addChild(pMenu,1);    CCSprite* pSprite = CCSprite::create("img/HelloWorld.png");    pSprite->setposition(ccp(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y));    this->addChild(pSprite,0);        return true;}voID HelloWorld::onhttpRequestServerInfoCompleted(CChttpClIEnt *sender,CChttpResponse *data){	CChttpResponse *response = (CChttpResponse*)data;	if (!response)	{		return;	}	if (0 != strlen(response->gethttpRequest()->getTag()))	{		cclOG("%s completed",response->gethttpRequest()->getTag());	}	int statusCode = response->getResponseCode();	const char *responseTag = response->gethttpRequest()->getTag();	char statusstring[64] = {};	sprintf(statusstring,"http status code:%d,tag = %s",statusCode,response->gethttpRequest()->getTag());	cclOG("response code:%d",statusCode);	if (!response->isSucceed())	{		cclOG("response Failed");		cclOG("error buffer:%s",response->getErrorBuffer());	}	std::vector<char> *buffer = response->getResponseData();	cclOG("http response,dump data:");	std::string result = "";	for (unsigned int i = 0; i < buffer->size(); i++)	{		result += (*buffer)[i];	}	// 打印如下	cclOG("*******************DEBUG PRINT*******************");	cclOG("result:%s",result.c_str());	cclOG("*******************DEBUG PRINT*******************");}voID HelloWorld::getServerInfo(){	std::string strUrl = "http://www.oschina.net/action/API/news_List";	CChttpRequest* request = new CChttpRequest();	request->setUrl(strUrl.c_str());	request->setRequestType(CChttpRequest::khttpPost);	std::vector<std::string> headers;	headers.push_back("Content-Type: application/Json; charset=utf-8");	request->setheaders(headers);	const char* postData = "catalog=2&pageIndex=1&pageSize=5";	request->setRequestData(postData,strlen(postData));	request->setResponseCallback(this,httpresponse_selector(HelloWorld::onhttpRequestServerInfoCompleted));	request->setTag("Post_My_Data:Server_Info");	CChttpClIEnt::getInstance()->send(request);	request->release();}voID HelloWorld::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)	CcmessageBox("You pressed the close button. windows Store Apps do not implement a close button.","Alert");#else	this->getServerInfo();    //CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif#endif}
总结

以上是内存溢出为你收集整理的Cocos2d-x_HTTP网络编程全部内容,希望文章能够帮你解决Cocos2d-x_HTTP网络编程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1051422.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存