我是使用Cococs2d-X
進行遊戲開發的新手。我試圖找到一些在我的遊戲中使用HttpClient
庫的例子。任何人都可以給我一個例子嗎?Http GET/POST請求在Coccos2d-X中
3
A
回答
1
這是擊中的getPost方法
cocos2d::extension::CCHttpRequest *request=new cocos2d::extension::CCHttpRequest();
request->setUrl("your url");
request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost);
request->setResponseCallback(this, httpresponse_selector(LoadingScene::yourCallBackMethod));
CCString *data = CCString::createWithFormat ("%s",yourData);
request->setRequestData(data->getCString(), data->length());
extension::CCHttpClient::getInstance()->send(request);
一個網址定義yourCallBackMethod這樣的基本代碼...
yourCallBackMethod(cocos2d::extension::CCHttpClient *sender,cocos2d::extension::CCHttpResponse *response){
std::vector<char> *buffer = response->getResponseData();
}
+0
錯誤「使用未聲明的標識符'擴展'」 – redux
0
這個代碼是基於cocos2d的的-X
測試//測試1
CCHttpRequest* request = new CCHttpRequest();
request->setUrl("http://just-make-this-request-failed.com");
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
request->setTag("GET test1");
CCHttpClient::getInstance()->send(request);
request->release();
/*******************************************************************************/
void HttpClientTest::onHttpRequestCompleted(CCHttpClient *sender, 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()
if (!response->isSucceed())
{
CCLog("response failed");
CCLog("error buffer: %s", response->getErrorBuffer());
return;
}
// dump data
std::vector<char> *buffer = response->getResponseData();
printf("Http Test, dump data: ");
for (unsigned int i = 0; i < buffer->size(); i++)
{
printf("%c", (*buffer)[i]);
}
printf("\n");
}
+0
錯誤「使用未聲明的標識符CCHttpClient」。建議我更改爲cocos2d :: network :: HttpClient,其錯誤爲「在嵌套名稱說明符中命名的不完整類型cocos2d :: network :: HttpClient」。 – redux
9
對於科科斯-2DX 3.3,你需要包含以下標題和使用的命名空間聲明:
#include "cocos2d.h"
#include "network/HttpClient.h"
USING_NS_CC;
using namespace network;
然後你就可以直接創建的HttpRequest:
HttpRequest* request = new HttpRequest();
request->setRequestType(HttpRequest::Type::GET);
request->setUrl("http://YOUR_URL");
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
request->setTag("Get test");
HttpClient::getInstance()->send(request);
request->release();
回調函數可以處理的響應:
void HttpClientTest::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response) {
if (response && response->getResponseCode() == 200 && response->getResponseData()) {
vector<char> *data = response->getResponseData();
string ret(&(data->front()), data->size());
CCLOG("%s", ("Response message: " + ret).c_str());
}
else {
CCLOG("%s", ("Error " + to_string(response->getResponseCode()) + " in request").c_str()); //error
}
}
相關問題
- 1. PHP - $請求 - >的getPost( 'FIRST_NAME')
- 2. http請求的HTTP請求
- 3. 在HTTP請求中修改HTTP請求行
- 4. 在AsyncTask中的HTTP請求
- 5. Jetty在http請求中返回400請求絕對URL請求
- 6. http請求在android
- 7. 在Xamarin.Forms HTTP請求
- 8. HTTP請求沒有請求在Elm 0.18
- 9. C#HTTP請求
- 10. HTTP GET請求()
- 11. Arduino http請求
- 12. HTTP請求
- 13. C++ HTTP請求
- 14. Android HTTP請求
- 15. InputStream Http請求
- 16. 對Http請求
- 17. Pushbullet HTTP請求
- 18. Http Put請求
- 19. Airplay http請求
- 20. Angular2,HTTP請求
- 21. 從http請求
- 22. Http請求
- 23. HTTP POST請求
- 24. DNS HTTP請求
- 25. swift HTTP請求
- 26. UTL_HTTP http請求
- 27. HTTP DELETE請求
- 28. HTTP POST請求
- 29. php http請求
- 30. AbstractTransactionalJUnit4SpringContextTests HTTP請求
是否真的很難檢查其網站上的cocos2d-x文檔? http://www.cocos2d-x.org/wiki/How_to_use_CCHttpClient – Morion
當網站上的代碼無法編譯,而且您不知道如何修復時,很難。 – redux