cocos2d-x学习日记之网络通信篇—HttpClient
2016-10-10 19:30
393 查看
Title:使用HttpClient实现一个与服务器的简单的交互 通过输入账号和密码点击登录 显示登录情况
服务器地址:这里使用的是公司的服务器地址 "http://42.96.151.161:83/service/userlogin?user_name="+ _username+"&user_password="+
_password;
编译器:XCode
具体实现流程:
1.获取服务器中已经存在的数据
2.建立一个HelloWorld的类 实现一个简单的登录界面
3.输入用户名和密码 通过HttpClient实现与服务器的交互
创建一个HelloWorld的类 在HelloWorld.h添加以下代码
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "network/HttpClient.h"
using namespace cocos2d;
using namespace ui;
using namespace network;
class HelloWorld :
public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual
bool init();
void loginButton(Ref* sender);
void responseCallback(HttpClient* sender,HttpResponse* response);
CREATE_FUNC(HelloWorld);
private:
TextField* username;
TextField* password;
Label* label;
};
#endif // __HELLOWORLD_SCENE_H__
在HelloWorld.cpp下添加以下代码
#include "HelloWorldScene.h"
#include "external/curl/include/ios/curl/curl.h"
//rapidjson文件操作的头文件
#include "json/rapidjson.h"
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
using namespace rapidjson;
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return
false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
//添加编辑框和登录按钮
username=TextField::create();
username->setBrightStyle(Widget::BrightStyle::NORMAL);
username->setFontSize(40);
username->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2+200));
this->addChild(username);
password=TextField::create();
password->setBrightStyle(Widget::BrightStyle::NORMAL);
password->setFontSize(40);
password->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2+140));
this->addChild(password);
auto login_button=Button::create();
login_button->loadTextureNormal("button_n.png");
login_button->loadTexturePressed("button_p.png");
login_button->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2+60));
login_button->setTitleText("Login");
login_button->setTitleFontSize(30);
login_button->addClickEventListener(CC_CALLBACK_1(HelloWorld::loginButton,this));
this->addChild(login_button);
label=Label::createWithTTF(" ","fonts/Marker Felt.ttf",
50);
label->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this->addChild(label);
return
true;
}
void HelloWorld:: loginButton(Ref* sender)
{
std::string _username=username->getString();
std::string _password=password->getString();
std::string url="http://42.96.151.161:83/service/userlogin?user_name="+ _username+"&user_password="+ _password;
HttpRequest* request=new HttpRequest();
request->setUrl(url.c_str());
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(HelloWorld::responseCallback ,this));
HttpClient::getInstance()->send(request);
request->release();
}
void HelloWorld:: responseCallback(HttpClient* sender,HttpResponse* response)
{
std::vector<char>* data=response->getResponseData();
char c[data->size()];
for (int i=0; i<data->size(); i++) {
c[i]=data->at(i);
}
c[data->size()]='\0';
log("%s",c);
std::string path=FileUtils::getInstance()->getWritablePath();
path += "cookies.json";
if(!FileUtils::getInstance()->isFileExist("cookies.json"))
{
FILE* file=fopen(path.c_str(), "w");
fputs(c, file);
fflush(file);
fclose(file);
}
Document doc;
doc.Parse<rapidjson::kParseDefaultFlags>(c);
rapidjson::Value& status=doc["status"];
int i=status.GetInt();
if (i==1) {
label->setString("login succeed");
log("login succeed");
}
else{
label->setString("login failed");
log("login failed");
}
log("Path:%s",path.c_str());
}
服务器地址:这里使用的是公司的服务器地址 "http://42.96.151.161:83/service/userlogin?user_name="+ _username+"&user_password="+
_password;
编译器:XCode
具体实现流程:
1.获取服务器中已经存在的数据
2.建立一个HelloWorld的类 实现一个简单的登录界面
3.输入用户名和密码 通过HttpClient实现与服务器的交互
创建一个HelloWorld的类 在HelloWorld.h添加以下代码
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "network/HttpClient.h"
using namespace cocos2d;
using namespace ui;
using namespace network;
class HelloWorld :
public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual
bool init();
void loginButton(Ref* sender);
void responseCallback(HttpClient* sender,HttpResponse* response);
CREATE_FUNC(HelloWorld);
private:
TextField* username;
TextField* password;
Label* label;
};
#endif // __HELLOWORLD_SCENE_H__
在HelloWorld.cpp下添加以下代码
#include "HelloWorldScene.h"
#include "external/curl/include/ios/curl/curl.h"
//rapidjson文件操作的头文件
#include "json/rapidjson.h"
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
using namespace rapidjson;
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return
false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
//添加编辑框和登录按钮
username=TextField::create();
username->setBrightStyle(Widget::BrightStyle::NORMAL);
username->setFontSize(40);
username->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2+200));
this->addChild(username);
password=TextField::create();
password->setBrightStyle(Widget::BrightStyle::NORMAL);
password->setFontSize(40);
password->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2+140));
this->addChild(password);
auto login_button=Button::create();
login_button->loadTextureNormal("button_n.png");
login_button->loadTexturePressed("button_p.png");
login_button->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2+60));
login_button->setTitleText("Login");
login_button->setTitleFontSize(30);
login_button->addClickEventListener(CC_CALLBACK_1(HelloWorld::loginButton,this));
this->addChild(login_button);
label=Label::createWithTTF(" ","fonts/Marker Felt.ttf",
50);
label->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this->addChild(label);
return
true;
}
void HelloWorld:: loginButton(Ref* sender)
{
std::string _username=username->getString();
std::string _password=password->getString();
std::string url="http://42.96.151.161:83/service/userlogin?user_name="+ _username+"&user_password="+ _password;
HttpRequest* request=new HttpRequest();
request->setUrl(url.c_str());
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(HelloWorld::responseCallback ,this));
HttpClient::getInstance()->send(request);
request->release();
}
void HelloWorld:: responseCallback(HttpClient* sender,HttpResponse* response)
{
std::vector<char>* data=response->getResponseData();
char c[data->size()];
for (int i=0; i<data->size(); i++) {
c[i]=data->at(i);
}
c[data->size()]='\0';
log("%s",c);
std::string path=FileUtils::getInstance()->getWritablePath();
path += "cookies.json";
if(!FileUtils::getInstance()->isFileExist("cookies.json"))
{
FILE* file=fopen(path.c_str(), "w");
fputs(c, file);
fflush(file);
fclose(file);
}
Document doc;
doc.Parse<rapidjson::kParseDefaultFlags>(c);
rapidjson::Value& status=doc["status"];
int i=status.GetInt();
if (i==1) {
label->setString("login succeed");
log("login succeed");
}
else{
label->setString("login failed");
log("login failed");
}
log("Path:%s",path.c_str());
}
相关文章推荐
- Cocos2d-X学习——CCHttpClient网络访问被防火墙拦截
- cocos2d-x-2.0.4的网络连接库CCHttpClient的BUG
- cocos2d-x CCHttpClient 访问网络
- android 学习随笔十一(网络:HttpClient框架)
- 【Cocos2d-x】Cocos2d-X网络编程-HttpRequest/HttpClient/HttpResponse
- cocos2d-x学习篇之网络(http)篇
- 【cocos2dx学习笔记】HttpClient网络请求
- android 学习随笔十一(网络:HttpClient框架)
- cocos2d-x学习篇之网络(http)篇
- 【COCOS2DX-LUA 脚本开发之八】不用CCHttpClient而转用quick-cocos2d-x或cocos2dx-LuaProxy的Lua框架实现Lua-Http网络交互!
- cocos2d-x学习篇之网络(http)篇
- HttpClient java发送网络请求的学习
- Android学习指南之三十:使用URLConnection和HttpClient访问网络的方法
- 【COCOS2DX-LUA 脚本开发之八】不用CCHttpClient而转用quick-cocos2d-x或cocos2dx-LuaProxy的Lua框架实现Lua-Http网络交互!
- 【Cocos2d-x】 HttpClient 网络通信(Http)的简单应用
- cocos2d-x学习篇之网络(http)篇
- lua教程八----不用CCHttpClient而转用quick-cocos2d-x或cocos2dx-LuaProxy的Lua框架实现Lua-Http网络交互!
- Android网络框架学习之HttpClient
- Android之okHttpClient+handler+LruCache缓存网络图片学习笔记(通用MVP模式)
- Android 网络操作学习HttpURLConnection与HttpClient及网络框架选择