首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

flex发送请求到靠山

2012-11-23 
flex发送请求到后台!--StartFragment --交谈中请勿轻信汇款、中奖信息、陌生电话,勿使用外挂软件。???BadBo

flex发送请求到后台
<!--StartFragment -->flex发送请求到靠山交谈中请勿轻信汇款、中奖信息、陌生电话,勿使用外挂软件。

???BadBoy?2011-11-26?10:17:58
?Flex使用httpService,需要后台语言的支持,即类似JSP,PHP,ASP.NET,ASP等语言的支持,另外还涉及到部署WEB服务器的相关问题。Flex使用httpService,可以使我们在前台向后台的程序发送数据,并由后台经过处理后返回结果给前台。

?下面我将介绍如何使用httpService组件。

?首先,新建一个Application程序,文件名如httpServcieDemo.mxml,代码如下:
①用mxml方式
<?xml?version="1.0"?encoding="utf-8"?>
<mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"?borderColor="#1F80C3"?themeColor="#138DDB"?backgroundGradientAlphas="[1.0,?1.0]"?backgroundGradientColors="[#00C09A,?#0382AB]"?layout="absolute"?>
?<mx:Script>
??<![CDATA[
???import?mx.rpc.events.FaultEvent;
???import?mx.rpc.events.ResultEvent;
???import?mx.controls.Alert;
???
???//发送出错时的处理函数
???public?function?onFalutHandler(event:FaultEvent):void
???{
????Alert.show("发送或者接收时出现问题!");
???}
???
???//接收到结果后处理的函数
???public?function?onGetResultHandler(event:ResultEvent):void
???{
????Alert.show("您接收到java传来的结果是:"?+?event.result.toString());
???}
???
???public?function?doSend():void
???{
?????httpService1.url?=?"http://localhost:8080/test/index.jsp?txtMessage=";?
?????httpService1.url?+=?txtMessage.text;
?????httpService1.send();
???}
??]]>
?</mx:Script>
?<mx:HTTPService?id="httpService1"??????useProxy="false"????resultFormat="text"???fault="onFalutHandler(event)"?result="onGetResultHandler(event)"/>
?<mx:Label?text="发送信息:"?color="#1261E4"?fontWeight="bold"?fontSize="16"?x="136"?y="53"/>
?<mx:TextInput?x="225"?y="57"?width="368"?id="txtMessage"/>?
?<mx:Button?label="发送"?x="336"?y="107"?id="buttonSend"?width="62"?color="#0945F1"?fontSize="13"?click="doSend();"/>?
</mx:Application>



②用ActionScript方式
package?com.zbkc.biz
{
import?com.adobe.serialization.json.JSON;

import?flash.net.URLRequest;
import?flash.net.URLVariables;

import?mx.controls.Alert;
import?mx.rpc.events.FaultEvent;
import?mx.rpc.events.ResultEvent;
import?mx.rpc.http.HTTPService;

public?final?class?Service
{
private?var?httpService:HTTPService;

public?function?Service()
{
httpService=new?HTTPService();
httpService.useProxy=false;
httpService.method="POST";
httpService.addEventListener(FaultEvent.FAULT,faultCallback);//发生错误的时候
}

public?function?send(url:String,params:URLVariables,doLogicFunction:Function):void{
// if(params){
// Alert.show(url+"请求数据:\n"+params.toString());
// }else{
// Alert.show(url+"请求数据:null");
// }
//增加监听事件
httpService.addEventListener(ResultEvent.RESULT,function?resultCallback(event:ResultEvent):void{
var?data:String?=?event.result.toString();
//Alert.show(url+"响应数据:\n"+data);
var?jd:Object?=?JSON.decode(data);
var?isSuccess:Boolean=jd.result;
if(!isSuccess){
if(jd.errorCode==-2000){
flash.net.navigateToURL(new?URLRequest("infolist.html"),?"_self");
}else{
Alert.show(jd.message);
}
}else{
doLogicFunction(jd.data);
}
});


httpService.url=url;

httpService.clearResult();
if?(params){
httpService.send(params);
}else{
httpService.send();
}
}

private?function?faultCallback(event:FaultEvent):void{
Alert.show(event.fault.faultString);
}
}
}

热点排行