是否可以在 Flex 中进行轻量级 REST 调用? [英] Is it possible to do lightweight REST calls in Flex?

查看:33
本文介绍了是否可以在 Flex 中进行轻量级 REST 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在转换 Flex 应用程序以使用一些 REST API.

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

mx.rpc.http.HTTPService 类添加到代码后,SWF 二进制输出从 175KB 增加到 260KB.这是一个不可接受的打击.

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.

有没有更好的方法可以从 Flex 应用程序执行轻量级 REST 调用?我们最好使用外部接口 JS 来从那里进行调用吗?

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?

推荐答案

flash.net.URLLoader 内置于运行时中,不会导致文件大小的任何增加.我之前用过它作为 JSON 客户端,所以你应该不会有任何问题.

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.

下面是一个非常简单的例子.请参阅有关 HTTP_STATUS 的文档HTTP_RESPONSE_STATUS 了解有关其限制的信息.

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);

这篇关于是否可以在 Flex 中进行轻量级 REST 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆