cocos2d-x学习日记之网络通信篇—HttpClient

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());

 }

 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值