1、在头文件中:
#include
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Scene.h"
#include"libjson.h"
USING_NS_CC;
USING_NS_CC_EXT;
class ResigisterLayer:public CCLayer
{
public:
virtual bool init();
SCENE_FUNC(ResigisterLayer);
CREATE_FUNC(ResigisterLayer);
void Resgister();
void Register1Click();
void LoginBackClick();
CCEditBox *edit;
CCEditBox *edit1;
CCEditBox *edit2;
//上传内容
void uploadMessage(CCString *_name,CCString *_password,CCString *_surepassword);
void Request();
void OnhttpRequestComplete(CCHttpClient *client,CCHttpResponse *response);
//解析
void PaseJson( const JSONNode &n);
};
2、 #include "Scene.h" 的Scene类
#include "ResigisterLayer.h"
#include"LoginLayer.h"
#include
#include
#include
#include"PaserJSon.h"
bool ResigisterLayer::init()
{
if(!CCLayer::init())
{
return false;
}
this->Resgister();
return true;
}
void ResigisterLayer::Resgister()
{
//加入背景
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
CCSprite *backSprite=CCSprite::create("register.png");
backSprite->setPosition(ccp(winSize.width/2, winSize.height/2));
this->addChild(backSprite);
//加入标签 昵称、密码、确认密码
CCSprite *scale1=CCSprite::create("nicheng.png");
scale1->setPosition(ccp(50, 280));
this->addChild(scale1);
CCSprite *scale2=CCSprite::create("mima2.png");
scale2->setPosition(ccp(50, 220));
this->addChild(scale2);
CCSprite *scale3=CCSprite::create("querenmima.png");
scale3->setPosition(ccp(60, 160));
this->addChild(scale3);
//加入文本框
//用户名输入框
CCScale9Sprite *scale4=CCScale9Sprite::create("UserNametextfield.png");
scale4->setPosition(ccp(250,280));
scale4->setPreferredSize(CCSizeMake(300, 100));
edit= CCEditBox::create(CCSizeMake(200,40), scale4);
edit->setPosition(ccp(250,280));
edit->setInputMode(kEditBoxInputModeAny);
edit->setPlaceHolder("请输入用户名:");
edit->setFontColor(ccc3(255, 0, 0));
this->addChild(edit);
//密码输入框
CCScale9Sprite *scale5=CCScale9Sprite::create("passwordtextfield.png");
scale5->setPosition(ccp(250,220));
scale5->setPreferredSize(CCSizeMake(300, 100));
edit1= CCEditBox::create(CCSizeMake(200,40), scale5);
edit1->setPosition(ccp(250,220));
edit1->setInputMode(kEditBoxInputModeAny);
edit1->setInputFlag(kEditBoxInputFlagPassword);
edit1->setPlaceHolder("请输入密码:");
edit1->setFontColor(ccc3(255, 0, 0));
this->addChild(edit1);
//确认密码输入框
CCScale9Sprite *scale6=CCScale9Sprite::create("passwordtextfield.png");
scale6->setPosition(ccp(250,160));
scale6->setPreferredSize(CCSizeMake(300, 100));
edit2= CCEditBox::create(CCSizeMake(200,40), scale6);
edit2->setPosition(ccp(250,160));
edit2->setInputMode(kEditBoxInputModeAny);
edit2->setInputFlag(kEditBoxInputFlagPassword);
edit2->setPlaceHolder("请输入密码:");
edit2->setFontColor(ccc3(255, 0, 0));
this->addChild(edit2);
//添加按钮
CCMenuItemImage *resgister1=CCMenuItemImage::create("quedingzhuce_button2.png", "quedingzhuce_button.png", this, menu_selector( ResigisterLayer::Register1Click));
resgister1->setPosition(ccp(50,-50));
CCMenuItemImage *loginBack=CCMenuItemImage::create("fanhuidenglu_button2.png", "fanhuidenglu_button.png", this, menu_selector( ResigisterLayer::LoginBackClick));
loginBack->setPosition(ccp(-50,-50));
CCMenu *menu=CCMenu::create(resgister1,loginBack,NULL);
menu->setPosition(ccp(240,160));
this->addChild(menu);
}
void ResigisterLayer::LoginBackClick()
{
//CCTransitionFadeUp *fade=CCTransitionFadeUp::create(2, LoginLayer::scene());
CCLog("pppppppp");
CCDirector::sharedDirector()->replaceScene(LoginLayer::scene());
}
void ResigisterLayer::Register1Click()
{
this->Request();
//CCLog("dughfiuergiu");
}
void ResigisterLayer::Request()
{
CCString *name=CCString::create(edit->getText());
CCLog("%s",name->getCString());
CCString *password=CCString::create(edit1->getText());
CCLog("%s",password->getCString());
CCString *surepassword=CCString::create(edit2->getText());
CCLog("%s",surepassword->getCString());
if(name->length()*password->length()*surepassword->length()==0)
{
CCLog("asfd");
}
if(!(password->isEqual(surepassword)))
{
CCLog("asfdssss");
}
this->uploadMessage(name, password, surepassword);
}
//链接服务器
void ResigisterLayer::uploadMessage(CCString *_name,CCString *_password,CCString *_surepassword)
{
CCHttpRequest *request=new CCHttpRequest();
request->setUrl("http://42.96.151.161:83/service/registerUser");
request->setRequestType(CCHttpRequest::kHttpPost);
request->setResponseCallback(this, httpresponse_selector(ResigisterLayer::OnhttpRequestComplete));
//request->setResponseCallback(this, httpresponse_selector(ResigisterLayer::OnhttpRequestComplete));
CCLog("%s--------%s",_name->getCString(),_password->getCString());
char data[100];
sprintf(data, "user_name=%s&user_password=%s",_name->getCString(),_password->getCString());
printf("%s",data);
request->setRequestData(data, strlen(data));
request->setTag("PUST Test1");
CCHttpClient::getInstance()->send(request);
request->release();
}
//服务器的事件
void ResigisterLayer::OnhttpRequestComplete(CCHttpClient *client,CCHttpResponse *response)
{
// if(!response)
// {
// return;
// }
// if(0 != strlen(response->getHttpRequest()->getTag()))
// {
// CCLog("%s completed",response->getHttpRequest()->getTag());
// }
// int statusCode=response->getResponseCode();
// char statusString[64]={};
// sprintf(statusString, "http status code:%d,tag=%s",statusCode,response->getHttpRequest()->getTag());
// CCLog("respose code: %d",statusCode);
// if(!response->isSucceed())
// {
// CCLog("response is faild");
// CCLog("error buffer:%s",response->getErrorBuffer());
// return;
// }
// std::vector
// printf("http test,dump data:");
// for(unsigned int i=0;i
// {
// printf("%c",(*buffer)[i]);
// }
// printf("\n");
if(!response)
{
return;
}
if(0!=strlen(response->getHttpRequest()->getTag()))
{
CCLog("%s completed",response->getHttpRequest()->getTag());
}
int statusCode = response->getResponseCode();
char statusString[64] = {};
sprintf(statusString, "Http Status Code:%d, tag = %s",statusCode,response->getHttpRequest()->getTag());
if(!response->isSucceed())
{
CCLog("response failed");
CCLog("error buffer:%s",response->getErrorBuffer());
return;
}
std::vector<char>* buffer = response->getResponseData();
printf("Http Test ,dump data: ");
//得到string类型的数据
std::string sum=accumulate(buffer->begin(), buffer->end(), std::string(""));
const char *aString=sum.c_str();
for(unsigned int i = 0; i
{
printf("%c",(*buffer)[i]);
}
std::cout<<std::endl;
JSONNode n=libjson::parse(aString);
// this->PaseJson(n);
A *a=new A();
a-> PaseJson(n);
delete [] a;
a=NULL;
}
/*
//解析的函数
void ResigisterLayer::PaseJson(const JSONNode & n)
{
JSONNode::const_iterator i=n.begin();
while (i !=n.end() )
{
if(i->type()==JSON_ARRAY || i->type()==JSON_NODE)
{
PaseJson(*i);
}
std::string node_name=i->name();
if(node_name=="status")
{
json_string status=i->as_string();
std::cout< } ++i; } } */