本次主要介绍flex获取数据的两种方式:
(1)httpService从后台获取,内容只能为xml格式。
发送httpService请求,httpService通信和java的ajax比较类似:
package com.boco.util.server
{
import flash.net.FileReference;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public class BasicPostHttpService
{
private var httpService:HTTPService;
private var method:Function; //回调函数
public function BasicPostHttpService()
{
}
public function getResultXML(xmlUrl:String,method:Function):void
{
this.method = method; //指定回调函数名称
httpService = new HTTPService(); //声明httpService对象
httpService.method="post"; //请求类型
httpService.useProxy = false;
httpService.resultFormat = HTTPService.RESULT_FORMAT_E4X; //解析xml的方式
httpService.addEventListener(ResultEvent.RESULT,httpResult); //返回结果的处理函数
httpService.addEventListener(FaultEvent.FAULT,showResult); //出错处理函数
httpService.url = xmlUrl; //请求路径
httpService.send(); //发送
}
public function showResult(event:FaultEvent):void{
Alert.show(event.fault.faultDetail);
}
public function httpResult(event:ResultEvent):void
{
resultXML(XML(event.result));
}
public function resultXML(xml:XML):void
{
method.call(this,xml); //回调
}
}
}
web.xml里面增加 servlet 配置:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.boco.server.FlexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.flex</url-pattern>
</servlet-mapping>
后台servlet处理,并返回xml数据。
//获取货架最高层数和最长列数
public void getSize(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{
StringBuffer sizeInfo = new StringBuffer();
Connection con =null;
PreparedStatement pstmt = null;
ResultSet rs = null;
sizeInfo.append("<?xml version='1.0' encoding='UTF-8'?>");
sizeInfo.append("<size>");
try {
con = DBUtil.getConnection();
String sql = "select (select max(childnum) from repository_location_table where type=2) as maxfloor,(select max(childnum) from repository_location_table where type=3) as maxline from dual";
pstmt = con.prepareCall(sql);
rs = pstmt.executeQuery();
while(rs.next()){
sizeInfo.append("<maxWidth>");
sizeInfo.append(""+rs.getInt("MAXFLOOR")+"");
sizeInfo.append("</maxWidth>");
sizeInfo.append("<maxHeight>");
sizeInfo.append(""+rs.getInt("MAXLINE")+"");
sizeInfo.append("</maxHeight>");
}
sizeInfo.append("</size>");
System.out.println(sizeInfo.toString());
} catch (Exception e) {
e.printStackTrace();
} finally{
if(con!=null){
DBUtil.closeCon(con);
}
}
resp.setCharacterEncoding("UTF-8");
resp.getWriter().print(sizeInfo);
}
前台收到xml后,就能采用e4x来解析xml。下篇介绍如何使用 e4x。
(2)读取xml文件中的数据。因为flex有沙箱隔离,所以xml文件不能在本地,只能在flex的域里面。(就是在同一个服务器的部署目录下面)
1在.mxml文件中,采用<model>标签很容易就能读取到。网络上有很多介绍。
2在.as文件中读取xml
try{
//System.useCodePage = true; //默认未unicode编码,设置为true,按照文本的格式读取。
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("com/boco/draw/repository.xml"));
xmlLoader.addEventListener(Event.COMPLETE,changeShowModel);
}catch(error:Error){
Alert.show("read xml error");
}
public function changeShowModel(e:Event):void{ var xml:XML; var loader:URLLoader = e.target as URLLoader; if(loader!=null){ xml = new XML(loader.data); } handDetaiXML(xml);//这个方法中就能处理xml了,一样的用e4x }