Last active
March 24, 2017 08:47
-
-
Save Tetz/8168677 to your computer and use it in GitHub Desktop.
HTTP Client for Cocos2dx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// As a onClick event | |
void HelloWorld::onClick() | |
{ | |
// Setting HTTP Request Header | |
std::vector<std::string> headers; | |
headers.push_back("Content-Type: application/json; charset=utf-8"); | |
// Creating a URL | |
cocos2d::extension::CCHttpRequest * request = new cocos2d::extension::CCHttpRequest(); | |
request->setUrl("http://example.com"); | |
// If you want to use GET method, replace kHttpPost to kHttpGet | |
request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost); | |
request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestCompleted)); | |
request->setRequestData(postData, strlen(postData)); | |
request->setHeaders(headers); | |
request->setTag("POST Request"); | |
// Execute | |
cocos2d::extension::CCHttpClient::getInstance()->send(request); | |
// Release the Connection | |
request->release(); | |
} | |
// Deal with the Response | |
void HelloWorld::onHttpRequestCompleted(cocos2d::CCNode * sender, void * data) | |
{ | |
cocos2d::extension::CCHttpResponse * response = (cocos2d::extension::CCHttpResponse *)data; | |
if (!response) | |
{ | |
return; | |
} | |
if (0 != strlen(response->getHttpRequest()->getTag())) | |
{ | |
CCLog("%s completed", response->getHttpRequest()->getTag()); | |
} | |
// Check the HTTP Status Code | |
int statusCode = response->getResponseCode(); | |
char statusString[64] = {}; | |
sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); | |
CCLog("response code: %d", statusCode); | |
// A connection failure | |
if (!response->isSucceed()) | |
{ | |
CCLog("response failed"); | |
CCLog("error buffer: %s", response->getErrorBuffer()); | |
return; | |
} | |
// The data will be placed in the buffer. | |
std::vector<char> * buffer = response->getResponseData(); | |
char * concatenated = (char *) malloc(buffer->size() + 1); | |
std::string s2(buffer->begin(), buffer->end()); | |
strcpy(concatenated, s2.c_str()); | |
// JSON Parser. Include "spine/Json.h". | |
Json * json = Json_create(concatenated); | |
const char * test1 = Json_getString(json, "test1", "default"); | |
const char * test2 = Json_getString(json, "test2", "default"); | |
// View the console | |
CCLog("HTTP Response : key 1 : %s", test1); | |
CCLog("HTTP Response : key 2 : %s", test2); | |
// Delete the JSON object | |
Json_dispose(json); | |
} |
use rapid json to parse data.
how can i parse header ???
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Thanks for the httpclient code, i am using your code to get data form server..
I am making a HTTP request to a server and I am getting data from the database. Now I want to parse the JSON data and get the data into variables like this:
std::vector * buffer = response->getResponseData();
char * concatenated = (char *) malloc(buffer->size() + 1);
std::string s2(buffer->begin(), buffer->end());
strcpy(concatenated, s2.c_str());
Json * json = Json_create(concatenated);
std::string test1 = Json_getString(json, "id", "default");
std::string test2 = Json_getString(json, "pass", "default");
CCLog("HTTP Response : id : %s", test1);
CCLog("HTTP Response : pass : %s", test2);
But I am getting an error on:
Json Json_getItem (Json *object, const char string) {
Json *c = object->child; //error on this line -- exc_bad_access
while (c && Json_strcasecmp(c->name, string))
c = c->next;
return c;
}
When I log...
log("%s",concatenated);
...it outputs data like this:
id:mt pass:thkr
that means data is coming from server..
So how to parse data and get id, pass value in different variable so i can use it later in my code..plz help already posted in cocos forum but no reply..