Question

i have my HTTPService, it looks like this :

    <s:HTTPService 
        id="setCustomerInstalledPackageService" 
        url="http://localhost:8090/myapp/servletName" 
        useProxy="false" method="POST"
        resultFormat="text"
        result="onResult(event)"
        fault="fault(event)"> 
    </s:HTTPService>


I want to make this code more versatile by reading the HOST and PORT from a property file. This way, if I change the host (or the port) of my web-service, I will not have to re-compile my flash source.
I've Searched the web a little bit, but could not find the answer... anyone?

thanks!

Was it helpful?

Solution

Not sure whether this is the most elegant solution; if someone knows a better way I'd be happy to learn. The main idea is to declare the HTTPService with some unique string that can be replaced afterwards. In my case, I've used __host__ and __port__. After reading the config file, I replace these strings with the values I get from the file.

the Main.mxml:

private function initApp():void
{
    var ldr:URLLoader = new URLLoader();
    ldr.addEventListener(Event.COMPLETE, onLoadPropsFile);
    ldr.load(new URLRequest("service-config.txt"));
}

private function onLoadPropsFile(e:Event):void
{
    var host:String;
    var port:String;

    var loadedText:String = URLLoader(e.target).data;
    var array:Array = loadedText.split('\r\n');
    for each(var entry:String in array)
    {
        var keyValuePair:Array = entry.split('=');
        var key:String = keyValuePair[0];
        var val:String = keyValuePair[1];
        if(key == 'host')
        {
            host = val;
        }
        if(key == 'port')
        {
            port = val;
        }
    }
    var value:Number = Number(loadedText);

    resolveServiceUrl(myService, host, port);
}

private function resolveServiceUrl(service:HTTPService, host:String, port:String):void
{
    service.url = service.url.replace('__host__', host);
    service.url = service.url.replace('__port__', port);
}

The initApp() is invoked by the

<s:Application xmlns:... 
             ...
             initialize="initApp();" >

The service declared this way:

<s:HTTPService 
    id="myService" 
    url="http://__host__:__port__/appName/..." 
    useProxy="false" method="POST" resultFormat="text" 
    result="onResult(event)"
    fault="fault(event)"> 
    <mx:request xmlns="">
        <method>search</method>
        <input>{input.text}</input> 
    </mx:request>         
</s:HTTPService>

and the service-config.txt is very simple:

host=localhost
port=8090

Hope it will help someone, someday...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top