cocos2d-x 3.1.1 学习笔记[11] http请求 + json解析
//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解析的更多相关文章
- [XMPP]iOS聊天软件学习笔记[一]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- cocos2d-x 3.1.1学习笔记[23]寻找主循环 mainloop
文章出自于 http://blog.csdn.net/zhouyunxuan cocos2d到底是怎样把场景展示给我们的,我一直非常好奇. 凭个人猜想,引擎内部的结构类似于这样 while(true ...
- cocos2d-x 3.1.1 学习笔记[3]Action 动作
这些动画貌似都非常多的样子,就所有都创建一次. 代码例如以下: /* 动画*/ auto sp = Sprite::create("card_bg_big_26.jpg"); Si ...
- cocos2d-x 3.1.1 学习笔记[2]Sprite 精灵
Sprite应该是用到最多的一个类吧.无法想像一个游戏没有精灵将怎样进行愉快的玩耍. Sprite继承于Node 和 TextureProtocol. Sprite是一个2d的图像. Sprite能够 ...
- cocos2d-x 3.1.1 学习笔记[21]cocos2d-x 创建过程
文章出自于 http://blog.csdn.net/zhouyunxuan RootViewController.h #import <UIKit/UIKit.h> @interfac ...
- cocos2d-x 3.1.1 学习笔记[4]GridActions 网格动画
文章写的 http://blog.csdn.net/zhouyunxuan 老样子.见代码. //GridActions can only used on NodeGrid auto nodeGri ...
- [XMPP]iOS聊天软件学习笔记[四]
昨天完成了聊天界面,基本功能算告一段落 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework git clone https://githu ...
- [XMPP]iOS聊天软件学习笔记[三]
今天做了好友界面,其实xmpp内部已经写好很多扩展模块,所以使用起来还是很方便的 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework gi ...
- [XMPP]iOS聊天软件学习笔记[二]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
随机推荐
- oracle 11g在大表中添加字段及默认值--加速
今天遇到这个问题了.简单的增加语句,默认SQLPLUS执行,却会超时. 要增加客户端的TIMEOUT时间才可以解决.(感觉超过两三分钟,默认超时30秒) 另外, 也可以用两步操作(1,增加字段,2,修 ...
- HDU 4857 逃生 【拓扑排序+反向建图+优先队列】
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 6、Django实战第6天:用户登录
今天开始,我们需要来写后台逻辑了.... 后台逻辑代码都是编写在views.py文件里面,今天要完成的登录功能,因此来编辑users.views.py 这里我们根据请求方法来判断分为2种情况,网页默认 ...
- CentOS 7 下nagios搭建记录
跟随 园子的文章搭建 http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html 1.遇 nagios插件地址迁移错误,记录解决. 2. ...
- java中为什么要用多线程
我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread).线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可能和其他线程共享一些资源,比 ...
- [Codeforces 30D] Kings Problem
Brief Intro: 有n+1个点,其中n个点在X轴上,求从第k个点出发最短的汉密尔顿路径 Solution: 分类讨论+逐个枚举 设dist(i)是第i个点到n+1的距离 cal1(l,r)是n ...
- 【枚举】URAL - 2081 - Faulty dial
//._. ... ._. ._. ... ._. ._. ._. ._. ._. //|.| ..| ._| ._| |_| |_. |_. ..| |_| |_| //|_| ..| |_. ._ ...
- [TC-FindingFriends]Finding Friends
题目大意: 给定一个长度为$n(n\le10^5)$的数列$A(A_i\le10^9)$,求最小的$k$满足存在一个长度至少为$m(m\le n)$的子串,对于串中的每一个数$A_i$,都至少存在一个 ...
- Java多线程——ReentrantLock源码阅读
上一章<AQS源码阅读>讲了AQS框架,这次讲讲它的应用类(注意不是子类实现,待会细讲). ReentrantLock,顾名思义重入锁,但什么是重入,这个锁到底是怎样的,我们来看看类的注解 ...
- Java高级架构师(一)第08节:基本业务功能和数据字典