原文地址:http://www.cnblogs.com/top5/archive/2010/01/30/1660097.html

 1 package
 2 {
 3      import flash.display.Sprite;
 4      import flash.net.URLLoader;
 5      import flash.net.URLRequest;
 6      import flash.net.URLVariables;
 7      import flash.net.URLRequestMethod;
 8      import flash.events.Event;
 9     
10     // 对JSON的支持
11      import com.adobe.serialization.json.JSON;
12     
13      public class Net extends Sprite
14     {
15          public function Net()
16         {
17             // 以文本形式与ASP.NET通信
18              showText();
19             
20             // 以XML形式与ASP.NET通信
21              showXml();
22             
23             // 以JSON形式与ASP.NET通信
24              showJSON();
25          }
26         
27         // 以文本形式与ASP.NET通信
28         function showText():void
29         {
30             var v:URLVariables = new URLVariables("name=webabcd&age=27");
31             var r:URLRequest = new URLRequest();
32              r.url = "http://localhost:1343/Web/Text.aspx";
33              r.method = URLRequestMethod.GET;
34              r.data = v;
35             
36             var l:URLLoader = new URLLoader();
37              l.load(r);
38              l.addEventListener(Event.COMPLETE, textCompleteHandler);
39          }
40         
41         function textCompleteHandler(event:Event):void
42         {
43            var l:URLLoader = URLLoader(event.target);
44             
45              trace(l.data);
46             // output: name: webabcd; age: 27
47          }
48         
49         // 以XML形式与ASP.NET通信
50         function showXml():void
51         
52         
53         function xmlCompleteHandler(event:Event):void
54         {
55             var l:URLLoader = event.target as URLLoader;
56             var xml:XML = new XML(l.data);
57             
58             for each(var v in xml.person)
59             
60             // output: 
61             // 姓名:webabcd;年龄:27;薪水:1000
62             // 姓名:webabcdefg;年龄:37;薪水:2000
63             // 姓名:webabcdefghijklmn;年龄:47;薪水:30
64          }
65         
66         // 以JSON形式与ASP.NET通信
67         function showJSON():void
68         {
69             var v:URLVariables = new URLVariables()
70             var r:URLRequest = new URLRequest();
71              r.url = "http://localhost:1343/Web/JSON.aspx";
72              r.method = URLRequestMethod.GET;
73              r.data = v;
74             
75             
76             var l:URLLoader = new URLLoader();
77              l.load(r);
78              l.addEventListener(Event.COMPLETE, jsonCompleteHandler);
79          }
80         
81         function jsonCompleteHandler(event:Event):void
82         {
83             var l:URLLoader = event.target as URLLoader;
84             
85             var v:* = JSON.decode(l.data);
86             
87              trace("姓名:" + v.Name + ";年龄:" + v.Age);
88             // output: 姓名:webabcd;年龄:27
89          }
90      }
91 }

 

 

posted on 2010-04-27 08:54  精思入神  阅读(235)  评论(0编辑  收藏  举报