I’m using gabriel’s as3httpclient for calls to a RESTful JSON API in a Flex 4.5 application. It seemed to work quite well, but now I’ve encountered a problem which I currently don’t know how to fix. I’m using the lib’s HTTPDataListener instead of the normal HTTPListener.
At one point I’m sending 3 requests directly following each other (let’s call them A_GET, B_GET, and C_GET). The response to the first request is properly processed, but the responses of the following two requests are mashed together:
onData(A_GET)
onDataComplete(A_GET)
onComplete(A_GET) // so far everything's fine and working as expected
onData(B_GET)
onData(C_GET)
onDataComplete(B_GET+C_GET) // <-- here be dragons!
onComplete(B_GET)
onComplete(C_GET)
The problem is that somehow the JSON data from the responses to B_GET and C_GET are concatenated after/while running through onData() and before hitting onDataComplete(). This is the reason that instead of
[{ /* GET RESPONSE */ }]
I get something like
[{ /* B_GET RESPONSE */ }][{ /* C_GET RESPONSE */ }]
which obviously makes Flash’s native JSON parser choke (resulting in a SyntaxError: Error #1132: Invalid JSON parse input. runtime error), and I also only enter the onDataComplete() handler once instead of once per request, which seriously messes with my program flow.
Can anyone please give me a pointer what’s going on here and why the responses are combined into one? Am I just missing something important? Is there a [easy] workaround that doesn’t require me to refactor half my RESTful program logic or even switch libraries to make that problem go away?
I couldn’t find the source of the problem so I just worked around it. I ran the whole response body through a function which would split it on
"][", add the missing brackets and then push each fragment into the following response handling mechanism.