ActionScript中Http请求
因我是用MVC模式来开发的,View和Control两层的相关代码就不列出来了,我们先来看看Model层代码
SecurityInfoModel.as
package app.securityInfo.model
{
import com.xlands.common.net.http.events.PureHttpEvent;
import com.xlands.common.net.http.service.PureHttpService;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
public class SecurityInfoModel extends EventDispatcher
{
public static const EVENT_GET_SECURITY_INFO_SUCC:String = "EVENT_GET_SECURITY_INFO_SUCC";
private var _securityInfo:XML;
public function SecurityInfoModel()
{
}
/**
* 根据id取用户信息
*/
public function getSecurityInfo($id:int):void{
var request:URLRequest = PureHttpService.getSecurityInfoById($id)
var action:XlandsPureHttpAction = new XlandsPureHttpAction(request);
action.addEventListener(PureHttpEvent.EVENT_REQUEST_COMPLETE, __onGetSecurityInfoSucc);
action.send();
}
private function __onGetSecurityInfoSucc(event:PureHttpEvent):void{
(event.currentTarget as EventDispatcher).removeEventListener(PureHttpEvent.EVENT_REQUEST_COMPLETE, __onGetSecurityInfoSucc);
_securityInfo = event.getResponseXML();
this.dispatchEvent(new Event(EVENT_GET_SECURITY_INFO_SUCC));
}
}
}
上上model层中用到了自定义的事件PureHttpEvent,主要用来下发事件时包装要传输的信息
PureHttpEvent.as
package com.xlands.common.net.http.events {
import flash.events.Event;
/**
*
*
*/
public class PureHttpEvent extends Event {
public static const EVENT_REQUEST_COMPLETE:String = "event_complete";
public static const EVENT_IO_ERROR:String = "event_io_error";
public static const EVENT_FINISH:String = "event_finish";
private var _responseXML:XML;
public function PureHttpEvent($type:String, $response:XML) {
super($type, false, false);
_responseXML = $response;
}
public function getResponseXML():XML {
return _responseXML
}
}
}
服务层提供接口
PureHttpService.as
package com.xlands.common.net.http.service {
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
/**
*
*
*/
public class PureHttpService {
public function PureHttpService() {
}
/**
* 根据id来取得用户信息
* @param $id
*/
public static function getSecurityInfoById($id:int):URLRequest {
var sendObj:Object = new Object();
sendObj.$id = $id;
return getSSORequest("/ssoc/accountsecurity/getSecurityInfo", $id);
}
private static function getSSORequest($url:String, $sendObj:Object=null):URLRequest{
return getRequest("http://www.yourwebsite/" + $url, $sendObj);
}
private static function getRequest($url:String, $sendObj:Object=null, $post:Boolean=true):URLRequest{
var request:URLRequest = new URLRequest();
request.url = $url;
if ($sendObj){
var variables:URLVariables = new URLVariables();
for (var key:String in $sendObj){
variables[key] = $sendObj[key];
}
request.data = variables;
}
if ($post){
request.method = URLRequestMethod.POST;
}
return request;
}
}
}
再来看看真正的请求是如何处理的
XlandsPureHttpAction.as
package com.xlands.common.net.http {
import com.xlands.common.net.http.events.PureHttpEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
/**
*
*
*/
public class XlandsPureHttpAction extends EventDispatcher {
private var _request:URLRequest;
public function XlandsPureHttpAction($request:URLRequest) {
_request = $request;
}
public function send():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlLoader.addEventListener(ProgressEvent.PROGRESS, onProgress);
urlLoader.load(_request);
}
private function onComplete(e:Event):void {
e.currentTarget.removeEventListener(Event.COMPLETE, onComplete);
e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
e.currentTarget.removeEventListener(ProgressEvent.PROGRESS, onProgress);
//analyzeResult(new XML((e.currentTarget as URLLoader).data))
if (this.hasEventListener(PureHttpEvent.EVENT_REQUEST_COMPLETE) == true) {
this.dispatchEvent(new PureHttpEvent(PureHttpEvent.EVENT_REQUEST_COMPLETE, new XML((e.currentTarget as URLLoader).data)));
}
doFinish();
}
private function analyzeResult($xml:XML):void {
var value:*;
if (($xml..success as XMLList).length()) {
value = $xml..success.toString();
} else if (($xml..account as XMLList).length()) {
/*这里很不好,很不统一,根据token获取用户信息
成功:
<?xml version="1.0" encoding="UTF-8"?>
<sso>
<account id="100000" comeFrom="XLANDS" sex="false" nickname="samyang" name="samyang" email="aaaa@163.com" enable="false"/>
</sso>
*/
value = $xml..account[0];
} else if (($xml..error as XMLList).length()) {
value = $xml..error.toString();
} else {
//服务器返回异常数据
throw new Error("the value which the server returns is invalidated!!");
return;
}
}
private function onIOError(e:IOErrorEvent):void {
e.currentTarget.removeEventListener(Event.COMPLETE, onComplete);
e.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
e.currentTarget.removeEventListener(ProgressEvent.PROGRESS, onProgress);
if (this.hasEventListener(PureHttpEvent.EVENT_IO_ERROR) == true) {
this.dispatchEvent(new PureHttpEvent(PureHttpEvent.EVENT_IO_ERROR, null));
} else {
trace("您的请求不存在,或者服务器繁忙,请稍候再试。");
}
doFinish()
}
private function onProgress(e:ProgressEvent):void {
}
private function doFinish():void {
if (this.hasEventListener(PureHttpEvent.EVENT_FINISH) == true) {
this.dispatchEvent(new PureHttpEvent(PureHttpEvent.EVENT_FINISH, null));
}
}
}
}