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
分享到:
相关推荐
研究flex的通信问题,写了一个flex的Socket与C#通信例子,类似QQ软件的客户端和服务器端。
Flex与C#(WinForm)通过FlashPlayer通讯的例子
FLEX与C#、PHP、JAVA语言通信全面解析
在c#中用flex做界面展示时,想要从c#调用flex的方法,或者flex调用c#方法,本文中详细描述清楚了。
flex与C# winform交互,实现了flex与winform窗体互相传递数据功能
Flex与Java实现通信,Flex与Java实现通信,Flex与Java实现通信
Flex用C#连接数据库和用J2EE连接数据库两种方法。
flex与java通信
相信读者在学习完《Flex与ActionScript 3程序开发》后,在使用Flex进行组件开发和数据处理、使用Flex高效制作皮肤、将Flex与Flash紧密结合、使用Flex与服务器端语言通信、对Flex应用进行优化等方面都会有深刻的理解...
Flex与ActionScript3程序开发》一书由易到难、由浅人深、循序渐进地介绍了Flex与ActionScript3程序开发的一般步骤与常用的程序技巧,内容通俗易懂,使用大量实例贯穿全书,力求以实战的形式让读者快速掌握知识点。...
flex C# 基于socket 通讯 应用程序源码 客户度用flex 服务端用winform 的通讯程序,可以互发消息。
在使用c#播放flex的swf文件时,想要从调用flex方法,或者希望从flex中调用到c#方法的话,看着我共享的教程文档和这里的源代码,很轻松就能学会了,话说自认为讲的很详细了,这个里面是C#端的代码和flex端的代码,...
flex4出来一段时间了,去年看了一段时间flex3... 功能描述:分别用代码实现了三种flex4与Java通信 三种方式: 1 flex 与普通java类通信RemoteObject 2 flex 与服务器交互HTTPService 3 flex与webservice交互WebService
介绍了flex 与各种开发语言的通信,非常实用
Flex 与服务器端通信 demo 包括flex 源码 和c#服务器端源码
基于FluorineFx的C#和Flex聊天室,采用Flex和C#开发
目录: Flex基础篇 1.走进Flex世界 2.Flex基础知识 3.语言基础 4.数据基础 5.事件驱动 视觉设计篇 1.Flex皮肤设计 2.... 3.Flex与Flash无缝衔接 4....Flex通信篇 与js通信 与php通信 与j2ee通信
本书由易到难、由浅人深、循序渐进地介绍了Flex与ActionScript3程序开发的一般步骤与常用的程序技巧,内容通俗易懂,使用大量实例贯穿全书,力求以实战的形式让读者快速掌握知识点。相信读者在学习完本书后,在...
相同的算法,不同的语言实现RSA加密...net 的加密类与flex crypto不通用,不能实现,c#加密,flex解密,且只能公匙加密、私匙解密。因此整理出两边通用的加密类。实现简单,可以公匙加密私匙解密,或私匙加密公匙解密。