首页 > 代码库 > cocos2d-x HttpClient

cocos2d-x HttpClient

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)

    

 

 

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 = http://www.mamicode.com/response->getResponseData(); "==========%s",res.c_str());     } }

5、打包Android

  1、需要在Android.mk添加

   LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static

   $(call import-module,network)