`

Flex构建REST的客户端(两种解决方案)

    博客分类:
  • FLEX
阅读更多
如果说这篇文章可以帮助你,那么我将很荣幸,呵呵

先了解下REST的操作方法:
Create a new resource:
    POST http://www.myserver.org/myResource (with the new contents of the resource as the POSTed payload)

Get the resource with ID=2:
    GET http://www.myserver.org/myResource/2

Update the resource with ID=2
    PUT http://www.myserver.org/myResource/2 (with the new contents of the resource as the PUT payload)

Delete the resource with ID=2
    DELETE http://www.myserver.org/myResource/2
解决方案[/color]
[color=red]方案一:RestHttpService

下载SWC路径:http://code.google.com/p/resthttpservice/downloads/list
将SWC添加到项目方法
查看我上一篇文章:http://yiranwuqing.iteye.com/admin/blogs/715413

使用方法,将SWC添加到项目中后,可以在页面中直接使用
<rest:RestHttpService
  id="postHttpService"
  host="localhost"
  port="8080"
  method="{RestHttpService.METHOD_POST}"
  path="/myResource"
  contentType="application/xml"
  result="myResultHandler(event)"
  fault="myFaultHandler(event)"/>
触发方法:
postHttpService.send(<myData>This is my data</myData>);
一个具体的例子:http://code.google.com/p/resthttpservice/wiki/Introduction

方案二:as3httpclientlib.
as3httpclientlib
下载地址:http://code.google.com/p/as3httpclientlib/download/list
例子:
var client:HttpClient = new HttpClient();
var uri:URI = new URI("http://some.host/");

client.listener.onData = function(event:HttpDataEvent):void {
  // Notified with response content in event.bytes as it streams in
};

client.listener.onComplete = function(event:HttpResponseEvent):void {
  // Notified when complete (after status and data)
};
//如果是post
var json:String = "{'foo':'bar'}";
var jsonData:ByteArray = new ByteArray();
jsonData.writeUTFBytes(json);
jsonData.position = 0;
var contentType:String = "application/json";

client.post(uri, jsonData, contentType);//post
//如果是multipart
var multipart:Multipart = new Multipart([
  new Part("key", objectName),
  new Part("Content-Type", contentType),
  new Part("AWSAccessKeyId", accessKey),
  new Part("Policy", policy),
  new Part("Signature", signature),
  new Part("file", data, contentType, [ { name:"filename", value:objectName } ]),
  new Part("submit", "Upload")
]);
client.postMultipart(uri, multipart);
//如果是Head
client.head(uri);
//如果是del
client.del(uri);
例子链接:http://code.google.com/p/as3httpclientlib/wiki/Examples
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics