码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d-x HttpClient

时间:2014-05-07 01:51:43      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:android   blog   tar   ext   get   int   

1、运行环境

     1、cocos2d-x3.0 win7 64位 vs2012

2、添加库

    1、添加libNetwork库,方法参照http://blog.csdn.net/st75033562/article/details/23303223中的添加库的方法,最好也把这里面的提到的3个库也给添加了。

    2、添加链接:右击项目->属性->连接器->输入->附加依赖项->添加(libNetWork.lib、libExtensions.lib、libcurl_imp.lib)

    

bubuko.com,布布扣

 

 

3、头文件

  #include "network/HttpClient.h"   #include "network/HttpRequest.h"

  USING_NS_CC;   using namespace network;

4、代码

 1、发送数据  

  HttpClient* httpClient = HttpClient::getInstance();     HttpRequest* httpReq =new HttpRequest();

    httpReq->setRequestType(HttpRequest::Type::GET);      httpReq->setUrl("http://www.baidu.com/");      httpReq->setResponseCallback(this,SEL_HttpResponse(&HelloWorld::httpReqFinished));      httpReq->setTag("FirstNet");      httpClient->setTimeoutForConnect(300);      httpClient->send(httpReq);        httpReq->release();

 2、回调解析

   void HelloWorld::httpReqFinished(Node* node,CCObject* obj){   HttpResponse* response = (HttpResponse*)obj;      if (!response->isSucceed())      {          log("Receive Error! %s\n",response->getErrorBuffer());          return ;       }        const char* tag = response->getHttpRequest()->getTag();      if ( 0 == strcmp("FirstNet",tag))      {          std::vector<char> *data = response->getResponseData();          int data_length =  data->size();          std::string res;          for (int i = 0;i<data_length;++i)          {              res+=(*data)[i];          }          res+=‘\0‘;     log("==========%s",res.c_str());     } }

5、打包Android

  1、需要在Android.mk添加

   LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static

   $(call import-module,network)

cocos2d-x HttpClient,布布扣,bubuko.com

cocos2d-x HttpClient

标签:android   blog   tar   ext   get   int   

原文地址:http://www.cnblogs.com/xhg986/p/3712144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!