#ifndef AssetsManagerEx
#define IRDownloadEx
#include “…/HNCommon/HNBaseType.h”
#include “platform/CCFileUtils.h”
#include “network/CCDownloader.h”
NS_CS_BEGIN
USING_CS;
USING_NS_CC;
typedef std::function<void(bool,int)> onDownloadCallback;
class IRDownloadEx : public Ref
{
public:
IRDownloadEx();
void createTask(std::string url,std::string path);
void setCallback(onDownloadCallback& callback);
private:
~IRDownloadEx();
onDownloadCallback _callback;
//! Downloader
std::unique_ptrcocos2d::network::Downloader _downloader;
virtual void onError(const network::DownloadTask& task,
int errorCode,
int errorCodeInternal,
const std::string& errorStr);
/** @brief Call back function for recording downloading percent of the current asset,
the progression will then be reported to user's listener registed in addUpdateProgressEventListener
@param total Total size to download for this asset
@param downloaded Total size already downloaded for this asset
@param url The url of this asset
@param customId The key of this asset
@warning AssetsManagerEx internal use only
* @js NA
* @lua NA
*/
virtual void onProgress(double total, double downloaded, const std::string &url, const std::string &customId);
/** @brief Call back function for success of the current asset
the success event will then be send to user's listener registed in addUpdateEventListener
@param srcUrl The url of this asset
@param customId The key of this asset
@warning AssetsManagerEx internal use only
* @js NA
* @lua NA
*/
virtual void onSuccess(const std::string &srcUrl, const std::string &storagePath, const std::string &customId);
};
NS_CS_END
#endif
//这是cpp
#include “IRDownloadEx.h”
NS_CS_BEGIN
USING_CS;
IRDownloadEx::IRDownloadEx()
{
network::DownloaderHints hints =
{
static_cast<uint32_t>(32),
45,
“.tmp”
};
_downloader.reset(new network::Downloader());
_downloader->onTaskError = std::bind(&IRDownloadEx::onError, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
_downloader->onTaskProgress = [this](const network::DownloadTask& task,
int64_t /bytesReceived/,
int64_t totalBytesReceived,
int64_t totalBytesExpected)
{
this->onProgress(totalBytesExpected, totalBytesReceived, task.requestURL, task.identifier);
};
_downloader->onFileTaskSuccess = [this](const network::DownloadTask& task)
{
this->onSuccess(task.requestURL, task.storagePath, task.identifier);
};
}
IRDownloadEx::~IRDownloadEx()
{
_downloader->onTaskError = (nullptr);
_downloader->onFileTaskSuccess = (nullptr);
_downloader->onTaskProgress = (nullptr);
}
void IRDownloadEx::setCallback(onDownloadCallback& callback)
{
_callback = callback;
}
void IRDownloadEx::createTask(std::string url,std::string path)
{
_downloader->createDownloadFileTask(url, path,url);
}
void IRDownloadEx::onError(const network::DownloadTask& task,
int errorCode,
int errorCodeInternal,
const std::string& errorStr)
{
_callback(false,-1);
}
void IRDownloadEx::onProgress(double total, double downloaded, const std::string &url, const std::string &customId)
{
int ret = downloaded/total*100;
_callback(false,ret);
}
void IRDownloadEx::onSuccess(const std::string &srcUrl, const std::string &storagePath, const std::string &customId)
{
_callback(true,100);
}
NS_CS_END
用createTask建立任务, 然后就进onError了 url和路径应该是没有问题的url http://wx.qlogo.cn/mmopen/9C2AF3YXhKfFh5TiaqXES29HDCYI7uEe2kSW4kHkDdBEymjDvfKric2tz91ssYb1UPWdr1Z5icwibxDO3KdgxApJic9XDL759N7GM/0
路径 writeblePath + ….