Flex与C#的一般处理程序通信

Flex代码:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    backgroundColor="0x414141"> 
       
<!--上面preload属性与下面外部CSS可以删除它
 preloader="com.preload.PreLoad"
<mx:Style source="yfskin/yflexskin.css"/>
-->  
 
  
<mx:Script> 
   <![CDATA[ 
    import mx.collections.ArrayCollection; 
    import flash.events.Event; 
    import mx.rpc.events.ResultEvent; 
    import mx.controls.Alert;
    import mx.events.CloseEvent;
    import com.adobe.serialization.json.JSON; 
   
    private function personJSON(event:ResultEvent):void 
    { 
        //get the raw JSON data and cast to String 
        
        var rawData:String = String(event.result); 
        //showMsg(rawData);
        var person = JSON.decode(rawData); 
        
        txtName.text = person.data[0].name; 
        txtGx.text = person.data[0].sex; 
        txtAddress.text = person.data[0].age; 
        txtNiceName.text = person.data[0].email;
        txtEmployees.text = "不是"; 

     	var persons:Array = person.data as Array; 
        var personsCollection:ArrayCollection = new ArrayCollection(persons); 
        dg.dataProvider = personsCollection;  
    } 
    
    private function showMsg(obj:String):void
    {
    	 var alert:Alert = Alert.show(
				obj,"Alert Window Title",
				Alert.OK|Alert.CANCEL|Alert.NO|Alert.YES,
				this);
    }
   
    private function managerJSON(event:ResultEvent):void 
    { 
    	 
        //获取原始JSON数据并输出到InputText控件上 
        var rawData:String = String(event.result); 
        var alert:Alert = Alert.show(
				rawData,"Alert Window Title",
				Alert.OK|Alert.CANCEL|Alert.NO|Alert.YES,
				this);
        var manager = JSON.decode(rawData); 
        txtName.text = manager.txt_name; 
        txtGx.text = manager.txt_gx; 
        txtAddress.text = manager.address; 
        txtNiceName.text = manager.nice_name; 
        txtEmployees.text = "是"; 
     
        //更新DataGrid 
        var employees:Array = manager.employees as Array; 
        var employeesCollection:ArrayCollection = new ArrayCollection(employees); 
        dg.dataProvider = employeesCollection; 
     } 
    
    private function onSelected():void 
    { 
        txtName.text = dg.selectedItem.txt_name; 
        txtGx.text = dg.selectedItem.txt_gx; 
        txtAddress.text = dg.selectedItem.address; 
        txtNiceName.text = ""; 
        txtEmployees.text = ""; 
    } 
   ]]> 
 </mx:Script> 
  <mx:HTTPService id="personRequest" url="http://localhost:5401/WebFlex/HandlerFlex.ashx" 
    showBusyCursor="true" useProxy="false" method="GET" resultFormat="text"  
    result="personJSON(event)" > 
    <mx:request xmlns=""> 
      <data>"true"</data>
    </mx:request> 
  </mx:HTTPService> 
  <mx:HTTPService id="managerRequest" url="http://localhost:5401/WebFlex/HandlerFlex.ashx"  
    showBusyCursor="true" useProxy="false" method="GET" resultFormat="text"  
    result="managerJSON(event)"> 
    <mx:request xmlns=""> 
      <getManager>"true"</getManager> 
    </mx:request> 
  </mx:HTTPService> 
  
  <mx:Panel x="0" y="0" width="730" height="240"  
     layout="absolute" title="JSON 数据源实例"> 
    <mx:DataGrid id="dg" x="210" y="10" width="505" rowCount="5" 
        rowHeight="25" enabled="true" editable="false" change="onSelected()">  
      <mx:columns> 
        <mx:DataGridColumn headerText="姓名" dataField="name"/> 
        <mx:DataGridColumn headerText="性别" dataField="sex"/> 
        <mx:DataGridColumn headerText="年龄" dataField="age"/> 
        <mx:DataGridColumn headerText="邮箱" dataField="email"/> 
      </mx:columns> 
    </mx:DataGrid> 
    
    <mx:Label x="10" y="12" text="姓名:"/> 
    <mx:TextInput id="txtName" x="50" y="11" width="150" editable="false"/> 
    <mx:Label x="10" y="41" text="关系:"/> 
    <mx:TextInput id="txtGx" x="50" y="40" width="150" editable="false"/> 
    <mx:Label x="10" y="74" text="住址:"/> 
    <mx:TextInput id="txtAddress" x="50" y="72" width="150" editable="false"/> 
    <mx:Label x="10" y="105" text="昵称:"/> 
    <mx:TextInput id="txtNiceName" x="50" y="103" width="150" editable="false"/> 
    <mx:Label x="10" y="136" text="屋主:"/> 
    <mx:TextInput id="txtEmployees" x="50" y="134" width="150"  
        editable="false" text="不是"/> 
    
    <mx:Button id="getPerson" x="270" y="165" label="侄女"  
       click="personRequest.send();"/> 
    <mx:Button id="getManager" x="380" y="165" label="我的家族"  
       click="managerRequest.send();"/> 
    
  </mx:Panel> 
</mx:Application>

 

 

C#代码:

<%@ WebHandler Language="C#" Class="HandlerFlex" %>

using System;
using System.Web;
using System.Net;
using System.Text;
using System.Threading;

public class HandlerFlex : IHttpHandler {

    private HttpContext con;
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        con = context;
        this.Start();
    }

    public void Start()
    {
        Thread t = new Thread(new ThreadStart(ListenRequest));
        t.Start();
    }

    private void ListenRequest()
    {
        HttpResponse response = con.Response;
        string msg = this.getJsonData();

        con.Response.Write(msg);
    }

    private string getJsonData()
    {
        StringBuilder jsonData = new StringBuilder();
        jsonData.Append("{\"data\":[{\"name\":\"谢刚\",\"sex\":\"男\",\"age\":\"21\",\"email\":\"xiegangthrille@126.com\"}]}");
        //jsonData.Append("{\"nice_name\":\"\u963f\u5065\",\"employees\":[{\"txt_name\":\"\u963f\u96ea\",\"txt_gx\":\"\u8001\u5a46\",\"email\":\"xuelove520@21cn.com\",\"address\":\"\u5e7f\u4e1c\u8087\u5e86\u5e7f\u5b81\u8c2d\u5e03\u6c34\u58f0\u5cad\"},{\"txt_name\":\"\u5b87B\",\"txt_gx\":\"\u513f\u5b50\",\"email\":\"0758yu@163.com\",\"address\":\"\u5730\u7403\u4e9a\u6d32\u4e2d\u56fd\u5e7f\u4e1c\u8087\u5e86\u5e7f\u5b81\u5357\u8857\"}],\"txt_name\":\"\u6b27\u5b97\u5065\",\"txt_gx\":\"\u67f1\u4eba\",\"email\":\"0758jian@163.com\",\"address\":\"\u592a\u9633\u7cfb\u7ecf\u706b\u661f\u5230\u5730\u7403\"}");
        return jsonData.ToString();
    }
    
    public void RespClient(HttpListenerResponse response, string msg)
    {
        try
        {
            byte[] buffer = Encoding.UTF8.GetBytes(msg);
            response.ContentType = @"text/xml";
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            output.Close();
        }
        catch (Exception e)
        {
            
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

 flex工程需要用到的库:http://code.google.com/p/as3corelib/downloads/list

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值