由于WebService我们暂时用不到,在这里不举例了。
1.httpService
不多说了,直接上代码:
Code
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
3 <mx:Script>
4 <![CDATA[
5 internal function doResult():void{
6 //返回结果存储在HttpService的lastResult中。需要通过id调用
7 var returnValue:String=callphp.lastResult.toString();
8 txt.text = returnValue;
9 }
10 ]]>
11 </mx:Script>
12 <mx:HTTPService showBusyCursor="true" id="callphp" result="doResult()" method="POST" url="http://192.168.100.100/flex/testphp/getIp.php">
13 <mx:request>
14 <message> <!--这里是参数的名称-->
15 {txt.text}<!--这里是参数的内容-->
16 </message>
17 </mx:request>
18 </mx:HTTPService>
19 <mx:TextArea id="txt" x="272" y="48" height="78" width="192"/>
20 <mx:Button x="335.5" y="134" label="callphp" click="callphp.send()"/><!--直接调用HttpService的id的send方法-->
21</mx:Application>
如果不传递参数,HttpService的标签中间不包含任何东西即可。
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
3 <mx:Script>
4 <![CDATA[
5 internal function doResult():void{
6 //返回结果存储在HttpService的lastResult中。需要通过id调用
7 var returnValue:String=callphp.lastResult.toString();
8 txt.text = returnValue;
9 }
10 ]]>
11 </mx:Script>
12 <mx:HTTPService showBusyCursor="true" id="callphp" result="doResult()" method="POST" url="http://192.168.100.100/flex/testphp/getIp.php">
13 <mx:request>
14 <message> <!--这里是参数的名称-->
15 {txt.text}<!--这里是参数的内容-->
16 </message>
17 </mx:request>
18 </mx:HTTPService>
19 <mx:TextArea id="txt" x="272" y="48" height="78" width="192"/>
20 <mx:Button x="335.5" y="134" label="callphp" click="callphp.send()"/><!--直接调用HttpService的id的send方法-->
21</mx:Application>
2.URLLoader+URLRequest+URLVariables
本例子参考了http://hereson.javaeye.com/blog/193390。
具体看代码中的注释吧。
Code
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3
4 <mx:Script>
5 <![CDATA[
6 import mx.controls.Alert;
7 //声明一个URLLoader类的对象
8 private var urlloader:URLLoader = new URLLoader();
9
10 private function callphp():void{
11 //给这个URLLoader对象注册监听器,当load完成时触发
12 urlloader.addEventListener(Event.COMPLETE, changeValue);
13 //以一个URLRequest实例为参数,调用load方法。把要访问的php页面网址作为URLRequest的参数
14 urlloader.load(new URLRequest('http://192.168.100.100/flex/testphp/getIp.php'));
15 }
16
17 //urlloader注册的事件触发后执行此函数
18 private function changeValue(event:Event):void{
19 //把数据显示出来,urlloader.data里面存放的是数据
20 text.text = urlloader.data;
21 }
22 ]]>
23 </mx:Script>
24 <mx:TextArea id="text" x="230" y="46" width="201" height="78"/>
25 <mx:Button x="296" y="132" label="get" click="callphp()"/>
26</mx:Application>
上面的代码是不传递参数的情况。如果需要传递参数,则需这样写:
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3
4 <mx:Script>
5 <![CDATA[
6 import mx.controls.Alert;
7 //声明一个URLLoader类的对象
8 private var urlloader:URLLoader = new URLLoader();
9
10 private function callphp():void{
11 //给这个URLLoader对象注册监听器,当load完成时触发
12 urlloader.addEventListener(Event.COMPLETE, changeValue);
13 //以一个URLRequest实例为参数,调用load方法。把要访问的php页面网址作为URLRequest的参数
14 urlloader.load(new URLRequest('http://192.168.100.100/flex/testphp/getIp.php'));
15 }
16
17 //urlloader注册的事件触发后执行此函数
18 private function changeValue(event:Event):void{
19 //把数据显示出来,urlloader.data里面存放的是数据
20 text.text = urlloader.data;
21 }
22 ]]>
23 </mx:Script>
24 <mx:TextArea id="text" x="230" y="46" width="201" height="78"/>
25 <mx:Button x="296" y="132" label="get" click="callphp()"/>
26</mx:Application>
Code
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3
4 <mx:Script>
5 <![CDATA[
6 import mx.controls.Alert;
7 //声明一个URLLoader类的对象
8 private var urlloader:URLLoader = new URLLoader();
9 //把要访问的php页面网址作为URLRequest的参数
10 private var urlrequest:URLRequest = new URLRequest('http://192.168.100.100/flex/testphp/getIp.php');
11 //声明一个URLVariables,这个变量是用来存储参数的
12 private var values:URLVariables = new URLVariables();
13
14 private function callphp():void{
15 //给这个URLLoader对象注册监听器,当load完成时触发
16 urlloader.addEventListener(Event.COMPLETE, changeValue);
17 //类似html表单中的method属性,将传输方式设为POST
18 urlrequest.method = URLRequestMethod.POST;
19 //添加参数,这个message是自定义的,即为参数名字
20 values.message="hello flex!";
21 //把参数绑定到urlrequest上
22 urlrequest.data = values;
23 //以一个URLRequest实例为参数,调用load方法。
24 urlloader.load(urlrequest);
25 }
26
27 //urlloader注册的事件触发后执行此函数
28 private function changeValue(event:Event):void{
29 //把数据显示出来,urlloader.data里面存放的是数据
30 text.text = urlloader.data;
31 }
32 ]]>
33 </mx:Script>
34 <mx:TextArea id="text" x="230" y="46" width="201" height="78"/>
35 <mx:Button x="296" y="132" label="get" click="callphp()"/>
36</mx:Application>
37
1<?xml version="1.0" encoding="utf-8"?>
2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3
4 <mx:Script>
5 <![CDATA[
6 import mx.controls.Alert;
7 //声明一个URLLoader类的对象
8 private var urlloader:URLLoader = new URLLoader();
9 //把要访问的php页面网址作为URLRequest的参数
10 private var urlrequest:URLRequest = new URLRequest('http://192.168.100.100/flex/testphp/getIp.php');
11 //声明一个URLVariables,这个变量是用来存储参数的
12 private var values:URLVariables = new URLVariables();
13
14 private function callphp():void{
15 //给这个URLLoader对象注册监听器,当load完成时触发
16 urlloader.addEventListener(Event.COMPLETE, changeValue);
17 //类似html表单中的method属性,将传输方式设为POST
18 urlrequest.method = URLRequestMethod.POST;
19 //添加参数,这个message是自定义的,即为参数名字
20 values.message="hello flex!";
21 //把参数绑定到urlrequest上
22 urlrequest.data = values;
23 //以一个URLRequest实例为参数,调用load方法。
24 urlloader.load(urlrequest);
25 }
26
27 //urlloader注册的事件触发后执行此函数
28 private function changeValue(event:Event):void{
29 //把数据显示出来,urlloader.data里面存放的是数据
30 text.text = urlloader.data;
31 }
32 ]]>
33 </mx:Script>
34 <mx:TextArea id="text" x="230" y="46" width="201" height="78"/>
35 <mx:Button x="296" y="132" label="get" click="callphp()"/>
36</mx:Application>
37
附php端代码:
1
<?
2 $para = $_POST [ ' message ' ];
3 echo $para . ' ' . $_SERVER [ ' REMOTE_ADDR ' ];
4 ?>
2 $para = $_POST [ ' message ' ];
3 echo $para . ' ' . $_SERVER [ ' REMOTE_ADDR ' ];
4 ?>