//http须要引入的头文件和命名空间
#include <network/HttpClient.h>
using namespace network;
//json须要引入的头文件
#include <json/document.h>
    //然后声名两个函数
void http();//用于运行http请求
void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);// http请求的回调
void Nice::http()
{
//创建一个get请求
HttpRequest* request = new HttpRequest();
request->setUrl("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=Gk0Bbh9H4iREjesugyEqySN7");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(Nice::onHttpRequestCompleted, this));
request->setTag("json");
HttpClient::getInstance()->send(request);
request->release(); //创建一个post请求
HttpRequest* request_post = new HttpRequest();
request_post->setUrl("http://httpbin.org/post");
request_post->setRequestType(HttpRequest::Type::POST);
std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8");
request_post->setHeaders(headers);
request_post->setResponseCallback(CC_CALLBACK_2(Nice::onHttpRequestCompleted, this));
//post数据
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
request_post->setRequestData(postData, strlen(postData));
request_post->setTag("POST test2");
HttpClient::getInstance()->send(request_post);
request_post->release();
} void Nice::onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response)
{
if (!response) {
return; } if (0 != strlen(response->getHttpRequest()->getTag())) {
log("%s completed",response->getHttpRequest()->getTag());
} long statusCode = response->getResponseCode();
char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %ld, tag = %s",statusCode,response->getHttpRequest()->getTag());
log("response code: %ld",statusCode); if (!response->isSucceed()) {
log("response failed");
log("error buffer: %s",response->getErrorBuffer());
return; } std::vector<char>* buffer = response->getResponseData();
printf("Http Test, dump data: ");
for (unsigned int i = 0 ; i < buffer->size();i++) {
printf("%c",(*buffer)[i]);
} //将std::vector(char)* 转换成 std::string的两种方法
std::string backStr = std::string(&(*buffer->begin()), buffer->size());
std::string anotherStr;
anotherStr.insert(anotherStr.begin(), buffer->begin(), buffer->end());
printf("%s\n", backStr.c_str());
printf("%s\n", anotherStr.c_str());
printf("\n"); if (strcmp(response->getHttpRequest()->getTag(), "json") == 0) {
rapidjson::Document doc;
doc.Parse<0>(backStr.c_str());
const rapidjson::Value& v = doc["status"];
printf("status is : %s",v.GetString());
const rapidjson::Value& dir = doc["results"];
if (dir.IsArray()) {
unsigned int i = 0;
const rapidjson::Value& city = dir[i]["currentCity"];
log("city is %s", city.GetString());
//多层測试
const rapidjson::Value& title = doc["results"][(unsigned int)0]["index"][(unsigned int)2]["title"];
log("the title is %s", title.GetString());
}
} }

cocos2d-x 3.1.1 学习笔记[11] http请求 + json解析的更多相关文章

  1. [XMPP]iOS聊天软件学习笔记[一]

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  2. cocos2d-x 3.1.1学习笔记[23]寻找主循环 mainloop

    文章出自于  http://blog.csdn.net/zhouyunxuan cocos2d到底是怎样把场景展示给我们的,我一直非常好奇. 凭个人猜想,引擎内部的结构类似于这样 while(true ...

  3. cocos2d-x 3.1.1 学习笔记[3]Action 动作

    这些动画貌似都非常多的样子,就所有都创建一次. 代码例如以下: /* 动画*/ auto sp = Sprite::create("card_bg_big_26.jpg"); Si ...

  4. cocos2d-x 3.1.1 学习笔记[2]Sprite 精灵

    Sprite应该是用到最多的一个类吧.无法想像一个游戏没有精灵将怎样进行愉快的玩耍. Sprite继承于Node 和 TextureProtocol. Sprite是一个2d的图像. Sprite能够 ...

  5. cocos2d-x 3.1.1 学习笔记[21]cocos2d-x 创建过程

    文章出自于  http://blog.csdn.net/zhouyunxuan RootViewController.h #import <UIKit/UIKit.h> @interfac ...

  6. cocos2d-x 3.1.1 学习笔记[4]GridActions 网格动画

    文章写的  http://blog.csdn.net/zhouyunxuan 老样子.见代码. //GridActions can only used on NodeGrid auto nodeGri ...

  7. [XMPP]iOS聊天软件学习笔记[四]

    昨天完成了聊天界面,基本功能算告一段落 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework git clone https://githu ...

  8. [XMPP]iOS聊天软件学习笔记[三]

    今天做了好友界面,其实xmpp内部已经写好很多扩展模块,所以使用起来还是很方便的 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework gi ...

  9. [XMPP]iOS聊天软件学习笔记[二]

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

随机推荐

  1. jquerycheckbox事件

    https://stackoverflow.com/questions/7031226/jquery-checkbox-change-and-click-event $(document).ready ...

  2. RESTful 接口实现简明指南

    REST 简介 REST 是一个术语的缩写,REpresentational State Transfer,中文直译「表征状态转移」,这是个很拗口的词.我的建议是先不要强行理解,直接看怎么做,等对实施 ...

  3. 理解和上手Redux

    顾名思义本文分两个部分,理解和上手,第一部分我先讲个故事,这个故事也许不是特别形象,但对大家理解Redux一定有所帮助.第二部分我举个例子. 先讲个故事: 一个餐厅(应用),我是顾客(用户),这个餐厅 ...

  4. django CXRF介绍

    CSRF(Cross-site request forgery)跨站请求伪造,是攻击者利用用户的身份操作用户帐户的一种攻击方式.和XSS攻击一样,存在巨大的危害性. 一.攻击方法 1.低级的CXRF攻 ...

  5. 湖南师范大学2018年大学生程序设计竞赛新生赛 A 齐神和心美的游戏【hash】

    [链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: ...

  6. RPD Volume 168 Issue 4 March 2016 评论6

    Natural variation of ambient dose rate in the air of Izu-Oshima Island after the Fukushima Daiichi N ...

  7. 【博弈论】【SG函数】Codeforces Round #188 (Div. 1) D. Game with Powers

    将整个游戏可以划分成若干个互不相交的子游戏. 每个子游戏的sg值只与其中的数的个数有关.而这个数不会超过30. 于是可以预处理出这个sg值表. 然后从1到n枚举,对<=sqrt(n)的部分,用个 ...

  8. 【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics

    三道题都很类似.给出1741的代码 #include<cstdio> #include<algorithm> #include<cstring> using nam ...

  9. [POI2005]Toy Cars

    题目大意: 有n种物品,地上有k个格子,p次操作. 每次操作要求将某一个指定的物品移动到任意一个格子中,同时你可以选择是否将格子中的某一个物品收起来,并消耗1的代价. 如果下达指令时,这个物品刚好在格 ...

  10. 求满足n^2>12000的n的最大值 Exercise05_13

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求满足n^2>12000的n的最大值 * */ public class Exercise05_13 { public ...