您的位置:首页 > 理论基础 > 计算机网络

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());
 }
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: