前端之家收集整理的这篇文章主要介绍了
Cocos2d-x_HTTP网络编程,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#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
}
原文链接:https://www.f2er.com/cocos2dx/343808.html