Need help to make a CCDictionary from data on server


#1

Hi

I have uploaded a properties.plist file on a webserver holding properties for my game and levels. I manage to read it using CCHttpRequest. I would like to get this data in a CCDictionary format for simple parsing - setting my game properties.
Can someone point me in the right direction for filling the CCDictionary with the data from the httprequest response?

(I´m pretty new to both cocos2dx and cpp)

Using cocos2d-x 2.1.4 developing for IOS and Android.

Will writing the response data to a temp file work? I have not tried this yet as I´m trying to avoid it if possible . It requires some extra setup to write files on both ios and Android? Just been reading about setting the paths, and it seemed to be an issue.

//This funciton is based on the examples found in samples/Cpp/TestCpp/Classes/ExtensionsTest/NetworkTest.

void LevelData::onHttpRequestCompleted(cocos2d::extension::CCHttpClient *sender, cocos2d::extension::CCHttpResponse *response)
{
    if (!response)
    {
        return;
    }

    // You can get original request type from: response->request->reqType
    if (0 != strlen(response->getHttpRequest()->getTag()))
    {
        CCLog("%s completed", response->getHttpRequest()->getTag());
    }

    int statusCode = response->getResponseCode();
//    char statusString[64] = {};
//    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
//    m_labelStatusCode->setString(statusString);
    CCLog("response code: %d", statusCode);

    if (!response->isSucceed())
    {
        CCLog("response failed");
        CCLog("error buffer: %s", response->getErrorBuffer());
        return;
    }

    // dump data
    std::vector *buffer = response->getResponseData();
    printf("Http Test, dump data: ");
    for (unsigned int i = 0; i < buffer->size(); i++)
    {
        printf("%c", (*buffer)[i]);
    }
    printf("\n");

   //Make a CCDictionary and fill it with the data from the response
   //There are some alternatives for creating a CCDictionary . I need to know how I can utilize the data from the response 
    CCDictionary *myDictAlt1 =  CCDictionary::create(); //I guess this would require myDictAlt1->setObject to fill in the data 
    CCDictionary *myDictAlt2 = CCDictionary::createWithContentsOfFile(const char *pFileName);  //can data from the response somehow be used directly in this method. Would be nice
    CCDictionary *myDictAlt3 = CCDictionary::createWithContentsOfFileThreadSafe(const char *pFileName); //can data from the response somehow be used directly in this method. Would be nice
    CCDictionary *myDictAlt2 = CCDictionary::createWithDictionary(cocos2d::CCDictionary *srcDict); 

   //Parse the CCDictionary and set game properites

}

#2

>Will writing the response data to a temp file work? I have not tried this yet as I´m trying to avoid it if possible
Yes. Fortunately Android and iOS filesystem is quite fast, so it’s should not slowdown your game. Happy coding.

P.S. Dictionary::createWithData(), which inits dictionary from data in memory, can be useful for cocos2d-x, but someone should implement it and send patch (pull request). Btw, in this case createWithContentsOfFile() can be implemented through createWithData().


#3

Thanks Sergey. Making a tempefile worked fine.


#4

Yes, creating a temp file can load CCDictionary from it. But why not add a function to load CCDictionary from buffer, not file?
CCDictMaker even hide in CCFileUtils.cpp deeply, this mean you’d better not to implement a function load from buffer.
Strange.


#5

It’s just a gap in cocos2dx API, there was no special reason.