Flash HTTPService:从属性文件读取连接数据

发布于 2024-12-22 07:55:53 字数 473 浏览 5 评论 0原文

我有我的 HTTPService,它看起来像这样:

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


我想通过从属性文件读取 HOST 和 PORT 来使此代码更加通用。这样,如果我更改网络服务的主机(或端口),我将不必重新编译我的闪存源。
我在网上搜索了一下,但找不到答案......有人吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2024-12-29 07:55:53

不确定这是否是最优雅的解决方案;如果有人知道更好的方法,我很乐意学习。
主要思想是使用一些可以在以后替换的唯一字符串来声明 HTTPService。就我而言,我使用了__主机____端口__。读取配置文件后,我将这些字符串替换为从文件中获取的值。

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);
}

调用

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

initApp() 由这样声明的服务

<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>

: service-config.txt 非常简单:

host=localhost
port=8090

希望有一天它能帮助某人......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文