//
Connection.h
Xxdemo
//
Created by Leehongee on 14-9-4.
//
//
#ifndef __xxdemo__connection__
#define __XXDEMO__CONNECTION__
#include <iostream>
#include "Cocos2d.h"
#include "Network/httpclient.h"
USING_NS_CC;
using namespace Std;
using namespace Cocos2d::network;
Class connection{
Public
void Dohttprequest (const char* url,const char* requestdata,const char* tag,const cchttprequestcallback& callback);
void onhttprequestimagecompleted (HttpClient *sender, HttpResponse *response);
Private
Get Request Network Picture Resource
texture2d* texture;
};
#endif/* Defined (__xxdemo__connection__) */
//
Connection.cpp
Xxxdemo
//
Created by Leehongee on 14-9-4.
//
//
#include "Connection.h"
/**
After compiling to Android platform, pay attention to adding network permissions
**/
void Connection::d ohttprequest (const char* url,const char* requestdata,const char* tag,const cchttprequestcallback & callback) {
httprequest* request = new HttpRequest ();
Request->seturl (URL);
Request->setrequesttype (httprequest::type::P ost);
This is the callback object and the callback function
Request->setresponsecallback (callback);
Request->settag (tag);
The requested data
if (strlen (requestData)!=0) {
Request->setrequestdata (Requestdata,strlen (requestData));
}
Httpclient::getinstance ()->send (request);
Freeing memory
Request->release ();
}
void connection::onhttprequestimagecompleted (HttpClient *sender, HttpResponse *response) {
if (!response)
{
Return
}
You can get original request type From:response->request->reqtype
Const char* tag = Response->gethttprequest ()->gettag ();
if (0! = strlen (tag))
{
Log ("%s completed", Response->gethttprequest ()->gettag ());
}
if (!response->issucceed ())
{
Log ("Error buffer:%s", Response->geterrorbuffer ());
Return
}
std::vector<char> *buffer = Response->getresponsedata ();
Create image
image* image = new Image;
Image->initwithimagedata (unsigned char*) buffer->data (), buffer->size ());
Create texture
Texture = new Texture2d ();
BOOL Isimage = texture->initwithimage (image);
if (isimage) {
Log ("Texture2d initialization!");
}
Image->release ();
}
COCOS2DX 3.x (load Web custom avatar)