Cocos2dx之http網路請求

君墨痕發表於2014-03-17

先包含進extensions目錄


CCHttpClient就是要用的類



客戶端程式碼:

先引入標頭檔案和名稱空間

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

然後直接上點選傳送按鈕的回撥函式請求網路

//點選按鈕的回撥函式
void TestLayer::btncallback(CCObject* pSender)
{
	bool requestType_is_get = false;//採用get方式或者post方式
	if (requestType_is_get)
	{
		CCHttpRequest* request = new CCHttpRequest();//建立請求物件
		string str1 = "http://127.0.0.1:8080/b_springmvc_cms/cocos2dx.do";
		string str2 = p_User_EditBox->getText();
		string str3 = p_Psw_EditBox->getText();
		string struser = "?username=";//注意在get方式請求後面加個?問號,再加上請求引數
		string strpsw = "&password=";
		str1 = str1 + struser + str2 + strpsw + str3;
		CCLOG("GetType data : %s", str1.c_str());
		request->setUrl(str1.c_str());//設定請求的url,username和password已經包含在url中
		request->setRequestType(CCHttpRequest::kHttpGet);//設定為Get模式
		request->setResponseCallback(this, httpresponse_selector(TestLayer::onHttpRequestCompleted));//設定響應的回撥
		request->setTag("getTag");//可以在回撥函式中取到這個tag字串
		CCHttpClient::getInstance()->send(request);//傳送請求
		request->release();//釋放請求
	}
	else
	{
		CCHttpRequest* request = new CCHttpRequest();//建立請求物件
		string str1 = "http://127.0.0.1:8080/b_springmvc_cms/cocos2dx.do";
		string str2 = p_User_EditBox->getText();
		string str3 = p_Psw_EditBox->getText();
		string struser = "username=";
		string strpsw = "&password=";
		str2 = struser + str2 + strpsw + str3;
		
		request->setUrl(str1.c_str());//設定請求的url,只是請求頁面的url,並不包含username和password
		request->setRequestType(CCHttpRequest::kHttpPost);//設定為Post模式
		request->setResponseCallback(this, httpresponse_selector(TestLayer::onHttpRequestCompleted));//設定響應的回撥
		const char* postData = str2.c_str();
		CCLOG("PostType data : %s", postData);

		request->setRequestData(postData, strlen(postData));//設定請求資料,也就是username和password
		request->setTag("postTag");//可以在回撥函式中取到這個tag字串
		CCHttpClient::getInstance()->send(request);//傳送請求
		request->release();//釋放請求
	}
}

接著就是響應的回撥函式

void TestLayer::onHttpRequestCompleted(CCHttpClient* client, CCHttpResponse* response)
{
	if (!response->isSucceed())//如果響應失敗,輸出錯誤資訊
	{
		CCString strError;
		strError.initWithFormat("Receive Error! \n%s\n", response->getErrorBuffer());
		m_labelStatusCode->setString(strError.getCString());
		return;
	}

	std::vector<char> *buffer = response->getResponseData();//接收響應資訊
	string recieveData;
	for (unsigned int i = 0; i < buffer->size(); i++)
	{
		recieveData += (*buffer)[i];
	}
	const char* tag = response->getHttpRequest()->getTag();
	CCLOG("Receive data : %s , tag=%s", recieveData.c_str(), tag);
	m_labelStatusCode->setString(recieveData.c_str());
}

服務端程式碼:

服務端用的是tomcat,控制層用springmvc框架搭建的,這裡指定了用只接受POST方式的請求

@Controller
public class Cocos2dxAction {

	@RequestMapping(value = "/cocos2dx.do",method=RequestMethod.POST)
	@ResponseBody
	public String cocos2dx(
			@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password) {
		System.out.println("recive success , username=" + username
				+ " , password=" + password);
		
		String string = "server message !";
		return string;
	}

}

效果:

請求前:



請求後:



eclipse控制檯輸出的資訊:



原始碼地址:http://download.csdn.net/detail/jackyvincefu/6713471

相關文章