我使用CCHttpRequest(cocos2dx)从互联网上请求照片,它适用于像“https://www.baidu.com/img/bdlogo.png”这样的网址。
但是我无法通过CCHttpRequest下载FB好友照片。照片网址为“https://graph.facebook.com/1386570461651640/picture ...” 此外,我尝试了NSRequest(OC)。它适用于此URL并下载了照片。
所以,任何人都知道如何通过CCHttpRequest下载朋友照片或任何其他适用于* .cpp文件的方法。
答案 0 :(得分:0)
此代码
string _id = "" // id require to whome you want to fectch photo
HttpRequest* request = new (std::nothrow) HttpRequest();
string url = "https://graph.facebook.com/"+_id+"/picture?height=120
&width=120";
request->setUrl(url.c_str());
request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(HellowWorld::onRequestImgCompleted, this));
request->setTag("GetImage");
HttpClient::getInstance()->send(request);
request->release();
void HellowWorld::onRequestImgCompleted(HttpClient *sender, HttpResponse *response)
{
log("AppDelegate::onHttpRequestCompleted - onHttpRequestCompleted BEGIN");
if (!response)
{
log("onHttpRequestCompleted - No Response");
return;
}
log("onHttpRequestCompleted - Response code: %lu", response->getResponseCode());
if (!response->isSucceed())
{
log("onHttpRequestCompleted - Response failed");
log("onHttpRequestCompleted - Error buffer: %s", response->getErrorBuffer());
return;
}
log("onHttpRequestCompleted - Response code: %s", response->getResponseDataString());
std::vector<char> *buffer = response->getResponseData();
Image * image = new Image ();
image-> initWithImageData ( reinterpret_cast<const unsigned char*>(&(buffer->front())), buffer->size());
Texture2D * texture = new Texture2D ();
texture-> initWithImage (image);
Sprite* sp = Sprite::createWithTexture(texture);
add(sp);
sp->setPosition(Vec2(100, 100));
}