Flex HTTPService

创建HTTPService对象

<s:HTTPService id="wordsData" url="http://hello.api.235dns.com/api.php?code=xml&key=bf8a2783362c94e153b5459eb124d0bb"
result="resultHandler(event)">

URL中如果使用&符号应写成&
例如上面的示例就应将url中内容改为http://hello.api.235dns.com/api.php?code=xml&amp;key=bf8a2783362c94e153b5459eb124d0bb

调用send()方法

创建HTTPService对象时并不会自动获取数据,要获取数据得向数据源发送请求。使用send()可以发送请求获取数据。

访问返回的数据

访问返回的数据有两种数据:
1. lastResult
第一种方式是通过HTTPService对象的lastResult属性访问数据例如

<s:HTTPService id="wordsData" url="http://hello.api.235dns.com/api.php?code=xml&key=bf8a2783362c94e153b5459eb124d0bb"/>

声明的HTTPService可以获得

<?xml version="1.0"?>
<xml>
    <id>116</id>
    <from>XiaoZhengRan</from>
    <time>1518</time>
    <day>1</day>
    <words>缘深多聚聚,缘浅随它去</words>
</xml>

需要访问xml中的words节点的数据,需要使用wordsData.lastResult.xml.words
但这种方法较为笨拙,所以开发中一般使用下面这种方法
2. result
通过以下方法声明HTTPService

<s:HTTPService id="wordsData" url="http://hello.api.235dns.com/api.php?code=xml&key=bf8a2783362c94e153b5459eb124d0bb"
result="resultHandler(event)">

在XML数据返回时会触发resultHandler()方法

import mx.rpc.events.ResultEvent;
private function resultHandler(event:ResultEvent):void{
    //使用`event.result.xml.words`的方法访问数据了。 
}

上面两种方法第一种可以在程序任何地方使用,儿第二种只能用方法的方式

你可能感兴趣的:(Flex HTTPService)