Search code examples
phpxmlhttprequestcocos2d-xpugixml

send/retrieve xml file to/from server cocos2d-x


My app saves all the data it needs onto an xml file that is stored on the device. When the user opens the app it pulls that xml file so they can keep their saved data.

I want to be able to upload that xml file to a server, specifically by using a php script with GET/POST calls if possible. I know how to send and retrieve single variable values but not sure how to send a file, specifically an xml file.

Am I able to use the cocos2d HttpRequest/HttpClient classes to achieve this? or is there a better way.

Thanks


Solution

  • I'm unfamiliar with cocos2d, but their website has an example of transferring a file via a POST request (which is the better method for transferring data such as XML).

    From the cocos3d-x.org wiki:

    cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest();
    request->setUrl("http://www.httpbin.org/post");
    request->setRequestType(cocos2d::network::HttpRequest::Type::POST);
    request->setResponseCallback( CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this) );
    
    // write the post data
    const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
    request->setRequestData(postData, strlen(postData));
    
    request->setTag("POST test1");
    cocos2d::network::HttpClient::getInstance()->send(request);
    request->release();
    

    In this example they're using the postData string as the data; you'll want to replace this with the XML contents.

    In addition you should include these standard headers:

    • Content-Type: application/xml; charset=utf-8 to indicate the type of data and it's encoding
    • Content-Length: 12389128 to show the length, in bytes, of the attached data

    You could also explore basic compression, which should significantly shrink the XML.