积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信

[索引页]
[源码下载]


积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信


作者:webabcd


介绍
Flash ActionScript 3.0 以文本形式与ASP.NET通信、以XML形式与ASP.NET通信和以JSON形式与ASP.NET通信


示例
Text.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Text : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
string s = "name: " + Request.QueryString["name"+ "; age: " + Request.QueryString["age"];

        Response.ClearContent();
        Response.ContentType 
= "text/plain";
        Response.Write(s);
        Response.End();
    }

}


Xml.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Xml : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
string s = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <root>
                <person name=""webabcd"" age=""27"">
                    <salary>1000</salary>
                </person>
                <person name=""webabcdefg"" age=""37"">
                    <salary>2000</salary>
                </person>
                <person name=""webabcdefghijklmn"" age=""47"">
                    <salary>3000</salary>
                </person>
            </root>
";

        Response.ClearContent();
        Response.ContentType 
= "text/xml";
        Response.Write(s);
        Response.End();
    }

}


JSON.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class JSON : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Person person 
= new Person();
        person.Name 
= "webabcd";
        person.Age 
= 27;

        HttpContext.Current.Response.ClearContent();
        
// HttpContext.Current.Response.ContentType = "application/json";
        HttpContext.Current.Response.ContentType = "text/plain";

        
// 把person对象序列化成JSON
        System.Runtime.Serialization.DataContractJsonSerializer dcjs = new System.Runtime.Serialization.DataContractJsonSerializer(person.GetType());
        dcjs.WriteObject(HttpContext.Current.Response.OutputStream, person);

        HttpContext.Current.Response.End();
    }

}


/// <summary>
/// Person类
/// </summary>

[System.Runtime.Serialization.DataContract]
public class Person
{
    
private string _name;
    
/// <summary>
    
/// 姓名
    
/// </summary>

    [System.Runtime.Serialization.DataMember]
    
public string Name
    
{
        
get return _name; }
        
set { _name = value; }
    }


    
private int _age;
    
/// <summary>
    
/// 年龄
    
/// </summary>

    [System.Runtime.Serialization.DataMember]
    
public int Age
    
{
        
get return _age; }
        
set { _age = value; }
    }

}



Net.as
package
{
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLRequestMethod;
    import flash.events.Event;
    
    
// 对JSON的支持
    import com.adobe.serialization.json.JSON;
    
    public class Net extends Sprite
    
{
        public 
function Net()
        
{
            
// 以文本形式与ASP.NET通信
            showText();
            
            
// 以XML形式与ASP.NET通信
            showXml();
            
            
// 以JSON形式与ASP.NET通信
            showJSON();
        }

        
        
// 以文本形式与ASP.NET通信
        function showText():void
        
{
            
var v:URLVariables = new URLVariables("name=webabcd&age=27");
            
var r:URLRequest = new URLRequest();
            r.url 
= "http://localhost:1343/Web/Text.aspx";
            r.method 
= URLRequestMethod.GET;
            r.data 
= v;
            
            
var l:URLLoader = new URLLoader();
            l.load(r);
            l.addEventListener(Event.COMPLETE, textCompleteHandler);
        }

        
        
function textCompleteHandler(event:Event):void
        
{
            
var l:URLLoader = URLLoader(event.target);
            
            trace(l.data);
            
// output: name: webabcd; age: 27
        }

        
        
// 以XML形式与ASP.NET通信
        function showXml():void
        
{
            
var v:URLVariables = new URLVariables()
            
var r:URLRequest = new URLRequest();
            r.url 
= "http://localhost:1343/Web/Xml.aspx";
            r.method 
= URLRequestMethod.GET;
            r.data 
= v;
            
            
            
var l:URLLoader = new URLLoader();
            l.load(r);
            l.addEventListener(Event.COMPLETE, xmlCompleteHandler);
        }

        
        
function xmlCompleteHandler(event:Event):void
        
{
            
var l:URLLoader = event.target as URLLoader;
            
var xml:XML = new XML(l.data);
            
            
for each(var v in xml.person)
            
{
                trace(
"姓名:" + v.@name + ";年龄:" + v.@age + ";薪水:" + v.salary);
            }

            
// output: 
            // 姓名:webabcd;年龄:27;薪水:1000
            // 姓名:webabcdefg;年龄:37;薪水:2000
            // 姓名:webabcdefghijklmn;年龄:47;薪水:30
        }

        
        
// 以JSON形式与ASP.NET通信
        function showJSON():void
        
{
            
var v:URLVariables = new URLVariables()
            
var r:URLRequest = new URLRequest();
            r.url 
= "http://localhost:1343/Web/JSON.aspx";
            r.method 
= URLRequestMethod.GET;
            r.data 
= v;
            
            
            
var l:URLLoader = new URLLoader();
            l.load(r);
            l.addEventListener(Event.COMPLETE, jsonCompleteHandler);
        }

        
        
function jsonCompleteHandler(event:Event):void
        
{
            
var l:URLLoader = event.target as URLLoader;
            
            
var v:* = JSON.decode(l.data);
            
            trace(
"姓名:" + v.Name + ";年龄:" + v.Age);
            
// output: 姓名:webabcd;年龄:27
        }

    }

}


OK
[源码下载]
posted @ 2007-11-15 08:19  webabcd  阅读(6185)  评论(47编辑  收藏  举报