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
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...