cocos2dx获取网络时间(二):浅析CCHttpClient(4)
来源:未知 责任编辑:责任编辑 发表时间:2015-03-01 01:33 点击:次
p> _pTarget = pTarget;
p> _pSelector = pSelector;
p>
p> if (_pTarget)
p> {
p> _pTarget->retain();
p> }
p> }
p>
p> 然后通过CCHttpClicent的send(CCHttpRequest* request)方法传入CCHttpRequest的实例,在异步请求Http完成后,调用注册的回调方法返回给我们Http请求的状态和数据。
p>
p> 初步了解了cocos2dx中回调的实现后,回到我们的NetTime中,在NetTime.h头文件中声明函数指针和用于注册的宏定义:
p>
p>
p>
p>typedef void (CCObject::*SEL_NetTime)(WebTime * pSender);
p>#define netTime_selector(_SELECTOR) (SEL_NetTime)(&_SELECTOR)
p> 同时声明用于存放调用者实例和函数指针的字段:
p>
p>SEL_NetTime _pSelector;
p>CCObject * _pTarget;
p> 通过重载requestNetTime方法传入回调方法的调用者和函数指针:
p>
p>void NetTime::requestNetTime(CCObject * pTarget, SEL_NetTime pSelector)
p>{
p> requestNetTime();
p> _pTarget = pTarget;
p> _pSelector = pSelector;
p>}
p> 在onHttpComplete方法的最后调用回调方法,并把NetTime的实例作为回调方法的参数:
p>
p>
p> 1 void NetTime::onHttpComplete(CCHttpClient * sender, CCHttpResponse * response)
p> 2 {
p> 3 CCHttpClient::getInstance()->release();
p> 4 if (!response)
p> 5 return;
p> 6 if (0 != strlen(response->getHttpRequest()->getTag()))
p> 7 {
p> 8 CCLog("%s completed", response->getHttpRequest()->getTag());
p> 9 }
p>10 int statusCode = response->getResponseCode();
p>11 char statusString[64] = {};
p>12 sprintf(statusString, "HTTP Status: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
p>13 CCLog("%s", statusString);
p>14 if (!response->isSucceed())
p>15 {
p>16 CCLog("response failed");
p>17 CCLog("error buffer:%s", response->getErrorBuffer());
p>18 return;
p>19 }
p>20 std::vector<char> * buffer = response->getResponseData();
p>21 std::string str;
p>22 for (unsigned i = 0; i < buffer->size(); i++)
p>23 {
p>24 char a = (*buffer)[i];
p>25 str.append(1, a);
p>26 }
p>27 CCLog("%s", str.c_str());
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>