引擎版本:cocos2dx 3.8+一,网络模块,接到任务的一般顺序1,对照接口文档,列出技术储备;2,对应技术储备列表,找出相关技术;3,写出demo,联调接口;4,检查逻辑,完善代码。
二,一些简单思绪短链接,长链接?http协议数据传输格式?json,xml数据编码?base64数据加密?md5
三,遇到的问题和具体解决方法1,HttpRequest头的设置先上代码: HttpRequest* request = new (std::nothrow) HttpRequest();
request->setUrl(ApiUtils::UserLoginUrl.c_str());
request->setRequestType(HttpRequest::Type::POST);
request->setResponseCallback(CC_CALLBACK_2(LoginScene::onHttpRequestCompletedLogin, this));
std::string postData;
ApiUtils::getInstance()->setQuickLogin(postData);
std::vector<std::string> headers;
headers.push_back("Content-Type: application/x-www-form-urlencoded; charset=utf-8");
request->setHeaders(headers);
request->setRequestData(postData.c_str(), strlen(postData.c_str()));
request->setTag("QuickLogin");
HttpClient::getInstance()->send(request);
request->release();
再说说遇到的问题:原来写的代码原本参考,官网给出的代码,没有加下面这句话 std::vectorstd::string headers; headers.push_back(“Content-Type: application/x-www-form-urlencoded; charset=utf-8”); request->setHeaders(headers);问题在于,在win32,android平台下,网络接口正常;但是在uwp上面,服务器反馈的结果就是参数为空。这个问题困扰我很久,把源码中所有的接口都看了一遍,然后看到有一个headers的设置,还有跟我对接的服务端(php)提示我说,我看php请求,都加了数据请求的类型。我想这两个应该能够对上。查了相关的资料,这个问题还真解决了。
http://blog.csdn.net/blueheart20/article/details/45174399
这个链接里面的类型比较全。
2,Base64编码和解码先上代码:std::string Config::base64_encode(const std::string& strTemp)
{
std::string dst = “”;
int len = 0;
char *buffer;
len = base64Encode((unsigned char*)strTemp.c_str(), (unsigned int)strTemp.length(), &buffer);
for (int i = 0; i < len; i++)
{
dst += buffer*;*
-
- }
return dst;
}
std::string Config::base64_decode(const std::string& strTemp)
{
std::string dst = “”;
int len = 0;
unsigned char *buffer;
len = base64Decode((unsigned char*)strTemp.c_str(), (unsigned int)strTemp.length(), &buffer);
for (int i = 0; i < len; i++)
{
dst += buffer;
}
return dst;
}
再说说遇到的问题:现在打代码比较懒,喜欢在引擎内部找,果然就找到了base64的方法。
3,rapidjson,字符串组装的改变 rapidjson::Document writedoc;
writedoc.SetObject();
rapidjson::Document::AllocatorType& allocator = writedoc.GetAllocator();
rapidjson::Value userValue(username.c_str(), allocator);
writedoc.AddMember("un", userValue, allocator);
记得之前的版本只需要这么写:writedoc.AddMember(“un”, username.c_str(),allocator);
