Cocos2d-x与服务器端数据交互(一)
从服务器端下载数据,保存到本地
加入Curl类库,在引擎中自带有
CurlUtils.h
#pragma once #include "curl.h" class CurlUtils { public: static void begin(); static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream); };
CurlUtils.cpp
#include "myheader.h" #include "CurlUtils.h" #include "curl.h" #include "cocos2d.h" using namespace std; using namespace cocos2d; void CurlUtils::begin() { CURL *curl; CURLcode res; curl = curl_easy_init(); if (curl!=NULL) { curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.104/Test/manager/addtest.action?userName=12&password=12"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); res = curl_easy_perform(curl); } curl_easy_cleanup(curl); } size_t CurlUtils::write_data(void *ptr, size_t size, size_t nmemb, void *stream) { FILE *fp=fopen("E:\\test.txt","w"); int written = fwrite(ptr, size, nmemb, (FILE *)fp); CCLog("close"); fclose(fp); return written; }
调用:CurlUtils::begin();
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30