actionscript 3 - Flex HTTPService : retrieve the service that sent the request -
does can tell me how can retrieve reference of httpservice object after http request done (success , fault)? here simple test application stores http requests. when handling response ("httpresult" , "httpfault" functions), seems impossible retrieve call succeed / failed.
this app output "call not found"
<?xml version="1.0" encoding="utf-8"?> <s:windowedapplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init(event);"> <fx:script> <![cdata[ import mx.events.flexevent; import mx.rpc.events.faultevent; import mx.rpc.events.resultevent; import mx.rpc.http.httpservice; private var calls:array; private function init(event:flexevent):void{ calls = new array(); // working http call test('http://v4.ipv6-test.com/api/myip.php'); // not working http call test('http://unknown.web.site.com/'); } private function test(url:string):void{ var service:httpservice = calls[ calls.push(new httpservice()) - 1]; service.url = url; service.method = 'get'; service.addeventlistener("result", httpresult); service.addeventlistener("fault", httpfault); service.send(); } private function httpresult(e:resultevent):void{ for(var i:int = calls.length; i>=0; i--){ if(calls[i]==e.target || calls[i]==e.currenttarget){ trace('successful http call found #' + i); return; } } trace('successful http call not found :('); } private function httpfault(e:faultevent):void{ for(var i:int = calls.length; i>=0; i--){ if(calls[i]==e.target || calls[i]==e.currenttarget){ trace('unsuccessful http call found #' + i); return; } } trace('unsuccessful http call not found :('); } ]]> </fx:script> <fx:declarations> <!-- placer ici les éléments non visuels (services et objets de valeur, par exemple). --> </fx:declarations> </s:windowedapplication>
i found out, thank eeerahul's answer (httpservice/resultevent flex 3.2 versus flex >= 3.5)
looks people develops flex want hide service when handling result. anyway there way retreive service's reference accessing private (or protected) property 'request' of event object. property, can correspond result service performed call.
the line fixed problem :
var curr_request:object = abstractoperation( e.currenttarget ).request;
the new working code :
<?xml version="1.0" encoding="utf-8"?> <s:windowedapplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init(event);"> <fx:script> <![cdata[ import mx.events.flexevent; import mx.rpc.events.*; import mx.rpc.events.faultevent; import mx.rpc.events.resultevent; import mx.rpc.http.abstractoperation; import mx.rpc.http.httpservice; private var calls:array; private function init(event:flexevent):void{ calls = new array(); // working http call test('http://v4.ipv6-test.com/api/myip.php'); // not working http call test('http://unknown.web.site.com/'); } private function test(url:string):void{ var service:httpservice = calls[ calls.push(new httpservice()) - 1]; service.url = url; service.method = 'get'; service.addeventlistener("result", httpresult); service.addeventlistener("fault", httpfault); service.send(); } private function httpresult(e:resultevent):void{ var curr_request:object = abstractoperation( e.currenttarget ).request; for(var i:int = calls.length-1; i>=0; i--){ if(calls[i].request==curr_request){ calls[i].removeeventlistener("result", httpresult); calls[i].removeeventlistener("fault", httpresult); trace('successful http call found #' + i); return; } } trace('successful http call not found :('); } private function httpfault(e:faultevent):void{ var curr_request:object = abstractoperation( e.currenttarget ).request; for(var i:int = calls.length-1; i>=0; i--){ if(calls[i].request==curr_request){ calls[i].removeeventlistener("result", httpresult); calls[i].removeeventlistener("fault", httpresult); trace('unsuccessful http call found #' + i); return; } } trace('unsuccessful http call not found :('); } ]]> </fx:script> <fx:declarations> <!-- placer ici les éléments non visuels (services et objets de valeur, par exemple). --> </fx:declarations> </s:windowedapplication>
Comments
Post a Comment