Is it possible to do lightweight REST calls in Flex?

时光总嘲笑我的痴心妄想 提交于 2020-01-01 07:00:08

问题


We are converting a Flex application to use some REST APIs.

When adding the mx.rpc.http.HTTPService class to the code, the SWF binary output grew from 175KB to 260KB. This is an unacceptable hit.

Is there any better way to do lightweight REST calls from a Flex app? Are we better off using an external interface JS just to make the calls from there?


回答1:


flash.net.URLLoader is built into the runtime and won't cause any increase in filesize. I've used it as a JSON client before, so you shouldn't have any troubles with it.

Below is a very simple example. See documentation for HTTP_STATUS and HTTP_RESPONSE_STATUS for information on their restrictions.

var request: URLRequest = new URLRequest("http://tempuri.org/service/json");
request.method = "POST";
request.contentType = "application/json";
request.data = JSON.encode(jsonObject);

var loader : URLLoader = new URLLoader(request);

// Only supported by some browsers
loader.addEventHandler(HTTPStatusEvent.HTTP_STATUS, statusCodeReceived);

// AIR only
loader.addEventHandler(HTTPStatusEvent.HTTP_RESPONSE_STATUS, statusCodeReceived);

loader.addEventHandler(Event.COMPLETE, function(ev:Event):void
{
    var responseJson : String = request.data as String;

    var responseJsonObject : String = JSON.decode(responseJson);
});

loader.addEventHandler(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader.addEventHandler(IOErrorEvent.IO_ERROR, errorHandler);



回答2:


I've always thought a good approach to this would be to create a small interface to the browser's JavaScript HTTP API, XmlHttpRequest. I've never tried it, but I've looked into it, and it looked like it might be fairly straightforward.

This would have the added benefit of working around Flash Player's security restrictions which make its HTTP support terribly crippled.



来源:https://stackoverflow.com/questions/4483098/is-it-possible-to-do-lightweight-rest-calls-in-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!