定义成全局或者静态成员函数
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
#include "curl\curl.h"
#include "network\HttpClient.h"
#include "network\HttpResponse.h"
USING_NS_CC;
USING_NS_CC_EXT;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
//分配点数请求回调函数
void onHttpRequestCompleted( CCHttpClient* client, CCHttpResponse* response);
static char *loginResponseString;
static size_t writehtml(void * ptr,size_t size,size_t number,void* stream);
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
char *HelloWorld::loginResponseString = NULL;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
//上传数据
//CCHttpRequest* request = new CCHttpRequest();//创建请求对象
//
//request->setUrl("http://192.168.1.11:3030/login");//设置请求的url,username和password已经包含在url中
//request->setRequestType(CCHttpRequest::kHttpGet);//设置为Get模式
//request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestCompleted));//设置响应的回调
//CCHttpClient::getInstance()->send(request);//发送请求
//request->release();//释放请求
return true;
}
size_t HelloWorld::writehtml(void * ptr,size_t size,size_t number,void* stream)
{
char *p = (char *)ptr;
loginResponseString = new char[strlen(p) + 1];
strcpy(loginResponseString, p);
return size*number;
}
//分配点数请求回调函数
void HelloWorld::onHttpRequestCompleted( CCHttpClient* client, CCHttpResponse* response){
if (!response->isSucceed())//如果响应失败,输出错误信息
{
CCMessageBox("请求失败", "");
return ;
}
std::vector *buffer = response->getResponseData();//接收响应信息
std::string recieveData;
for (unsigned int i = 0; i < buffer->size(); i++)
{
recieveData += (*buffer)[i];
}
CCMessageBox(recieveData.c_str(), "");
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CURL *url = curl_easy_init();
if(!url)
{//---if---
CCMessageBox("curl_easy_init网络连接失败", "失败");
return ;
}//---end if---
std::string cc;
curl_easy_setopt(url, CURLOPT_URL, "http://192.168.1.11:3030/login");
curl_easy_setopt(url, CURLOPT_POST, true);
curl_easy_setopt(url, CURLOPT_POSTFIELDS, "user_account=zhangsan&user_password=123456");
curl_easy_setopt(url, CURLOPT_FOLLOWLOCATION,1L);
curl_easy_setopt(url, CURLOPT_WRITEFUNCTION, (HelloWorld::writehtml));
curl_easy_setopt(url, CURLOPT_WRITEDATA,&cc);
if (CURLE_OK != curl_easy_perform(url))
{//---if---
CCMessageBox("curl_easy_perform失败", "失败");
return ;
}//---endif---
CCMessageBox(HelloWorld::loginResponseString, "");
}