HttpService工具类:
package common
{
import com.adobe.serialization.json.JSON;
import mx.controls.Alert;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.events.FaultEvent;
import mx.rpc.http.HTTPService;
public class HttpServices
{
private var http:HTTPService;
private var _host:String;
private static var https:HttpServices;
public function HttpServices()
{
http = new HTTPService();
}
public function set host(value:String):void
{
_host = value;
}
private function sendHttpSer(method:String,local:String,json:String,format:String,contentType:String):AsyncToken
{
_host = "http://192.168.1.146:8080";
local = _host + local;
http.url = local + method;
http.resultFormat = format;
http.method = "POST";
http.showBusyCursor = true;
http.contentType = contentType;
return json.length>0 ? http.send(json) : http.send();
}
public function sendHttpRequest(method:String,
resultFunc:Function,
local:String="/zport/dmd/YDBackScript/asset_handler?callMethod=",
format:String="e4x",
contentType:String="application/x-www-form-urlencoded"):void
{
var token:AsyncToken = sendHttpSer("getAllAssetsClass&submitted=true",local,"",format,contentType);
responderHandler(token,resultFunc);
}
public function sendJsonRequest(method:String,data:Object,resultFunc:Function,local:String="/zport/dmd/kwb_router"):void
{
var o:Object = {};
o.action = "KWBRouter";
o.method = method;
o.data = [data];
o.type = "rpc";
o.tid = 10;
var json:String = JSON.encode(o);
var token:AsyncToken = sendHttpSer("",local,json,"object","application/json; charset=utf-8");
responderHandler(token,resultFunc);
}
private function responderHandler(token:AsyncToken,resultHandler:Function):void
{
var responder:AsyncResponder = new AsyncResponder(resultHandler,faultHandler);
token.addResponder(responder);
}
private function faultHandler(event:FaultEvent,token:Object=null):void
{
// Alert.show("网络服务未能正常访问,请联系管理员解决,:)","错误提示");
trace( "FAULT: " + event.fault.message );
}
public static function getInstance():HttpServices
{
if(!https)https = new HttpServices();
return https;
}
}
}
调用方法:
https.sendHttpRequest("getAllAssetsClass&submitted=true",assetsClass_resultHandler);
private function getJsonData(param:Object,method:String,resultFunc:Function):void
{
var data:Object = {};
data.start = 0;
data.limit = 100;
data.param = param;
data.sort_param = {id:"asc"};
https.sendJsonRequest(method,data,resultFunc);
}
回调函数:
private function assetsClass_resultHandler(e:ResultEvent,token:Object=null):void
{
}