-1

Does someone can tell me how I can retrieve the reference of an HTTPService object after the HTTP request is done (success and fault)? Here is a simple Test Application that stores HTTP requests. When handling response ("httpResult" and "httpFault" functions), it seems impossible to retrieve which call succeed / failed.

This app output always "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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Simmoniz
  • 1,080
  • 15
  • 27
  • Looks like a duplicate : http://stackoverflow.com/questions/3009010/httpservice-resultevent-with-flex-3-2-versus-flex-3-5 – Simmoniz Apr 15 '14 at 13:49

1 Answers1

0

I found out, thank to eeerahul's answer (HTTPService/ResultEvent with Flex 3.2 versus Flex >= 3.5)

looks like people that develops Flex want to hide the service from us when handling the result. Anyway there is a way to retreive the service's reference by accessing the private (or protected) property 'request' of the event object. With that property, we can correspond the result with the service that performed the call.

The line that fixed my 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>
Community
  • 1
  • 1
Simmoniz
  • 1,080
  • 15
  • 27