http通讯问题

local request = network.createHTTPRequest(handler(self,self._onRecieve), url, “POST”)
request:addPOSTValue(“data”, data)
request:start()
print(“send------------------->”,msgname)

游戏用http通讯,其他消息都么有问题,只有一条消息每次都报这个错,想跟进代码看都不知道去哪看,求大神帮帮忙~
_onRecieve方法有的时候能执行到 event.name == “completed” 并且request:getResponseStatusCode() == 200,有的时候执行不到

NSConcreteAttributedString initWithString:: nil value
2014-07-22 23:10:29.432 player (
0 CoreFoundation 0x00007fff9180025c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff997aee75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff9180010c + + 204
3 Foundation 0x00007fff90eb06b5 - + 124
4 Foundation 0x00007fff90eb0575 - + 30
5 player 0x0000000106a644ae - + 398
6 player 0x0000000106a602aa - + 218
7 CoreFoundation 0x00007fff917cee0c CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER + 12
8 CoreFoundation 0x00007fff916c2a6d _CFXNotificationPost + 2893
9 Foundation 0x00007fff90e767ba - + 68
10 Foundation 0x00007fff90f6f450 _performFileHandleSource + 1548
11 CoreFoundation 0x00007fff91731731 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
12 CoreFoundation 0x00007fff91722ea2 __CFRunLoopDoSources0 + 242
13 CoreFoundation 0x00007fff9172262f __CFRunLoopRun + 831
14 CoreFoundation 0x00007fff917220b5 CFRunLoopRunSpecific + 309
15 HIToolbox 0x00007fff94240a0d RunCurrentEventLoopInMode + 226
16 HIToolbox 0x00007fff942407b7 ReceiveNextEventCommon + 479
17 HIToolbox 0x00007fff942405bc _BlockUntilNextEventMatchingListInModeWithFilter + 65
18 AppKit 0x00007fff984483de _DPSNextEvent + 1434
19 AppKit 0x00007fff98447a2b - + 122
20 AppKit 0x00007fff9843bb2c - + 553
21 AppKit 0x00007fff98426913 NSApplicationMain + 940
22 player 0x0000000106a63cd2 main + 34
23 player 0x0000000106a5b4d4 start + 52
)

是哪个版本 你对 event.name ~= "completed"做了处理没?

帮顶!:2:

没有做处理 跟服务端调了一下 以aascii码的形式打印出消息 好像是消息在一个地方因为有个0被截断了 不知道咋回事~

版本是2.1.5的

升级到2.2.5版本 如果不想升级直接拷贝底层 CCHTTPRequest.h 和 CCHTTPRequest.cpp这2个文件到你的2.1.5中,再看看。

说错了~ 是2.2.5 release版本的

event complete 的时候,并不一定 status == 200

解决的办法,是来一下服务返回的 http 头看一下

你可以看看下面的回调代码

innerCallback = function(event)
printf(" event:" … tostring(event.name))
local response = event.request
local statusCode
if event.name == “failed” then
statusCode = -1
else
statusCode = response:getResponseStatusCode()
end
printf(" event:" … tostring(event.name) … “, status:” … tostring(statusCode))
if event and event.name == “completed” then
if statusCode == 200 then
callback(nil, response:getResponseData())
else
callback(“bad server response. status:” … tostring(statusCode))
end
else
callback(“http request failed. error(” … tostring(response:getErrorCode()) … ") : " … tostring(response:getErrorMessage()))
end
end

2.2.5增加了进度条的功能,红色部分不能这样做处理。
if event and event.name == “completed” then
if statusCode == 200 then
callback(nil, response:getResponseData())
else
callback(“bad server response. status:” … tostring(statusCode))
end
else
callback(“http request failed. error(” … tostring(response:getErrorCode()) … ") : " … tostring(response:getErrorMessage()))
end

const string CCHTTPRequest::getResponseString(void)
{
CCAssert(m_state == kCCHTTPRequestStateCompleted, “CCHTTPRequest::getResponseString() - request not completed”);
return string(m_responseBuffer ? static_cast<char*>(m_responseBuffer) : “”);
}

void *CCHTTPRequest::getResponseData(void)
{
CCAssert(m_state == kCCHTTPRequestStateCompleted, “CCHTTPRequest::getResponseData() - request not completed”);
void *buff = malloc(m_responseDataLength);
memcpy(buff, m_responseBuffer, m_responseDataLength);
return buff;
}

原来是这的事 我调的getResponseString 但消息里边有个0 string(m_responseBuffer ? static_cast<char*>(m_responseBuffer) : “”);给截断了 改成getResponseData 就好了