• 【设为首页】
  • 【收藏闪客居】
当前位置:主页>AIR>文章内容
  • [翻译]AIR TIP4:调用SOAP Webservice
  • 来源:52ria.com 作者:稻草 2008-04-30 【

来源:AIR Tip 4: Calling a SOAP Webservice

这些日子,关于如何让单独的应用程序从互联网的不同位置获取数据成了一个普遍的话题。这是可以让AIR成为一个伟大的应用平台的特性中的一个。

在开始之前,我们需要创建一个非常简单的Coldfusion组件。这个组件包含了一个函数”getStuff”。如果你输入了你的名字,它就会返回一个字符串“你的姓名是…”。如果你把它放在你的网站上的某一个地方,就可以通过在文件名后添加”?wsdl”把它发布为一个webservice。这就是你把一个Coldfusion组件转换成一个SOAP协议的Webservice的所有步骤。

参考:Coldfusion 8 and Web Services

注意:我计划写一篇关于如何在JavaScript中使用Consuming Coldfusion Webservices的文章。这篇文章只是提到了这方面的一些基础。

Coldfusion:

  1. <cfcomponent>

  2. <cffunction name=“getStuff” access=“remote” returnType=“String”>

  3. <cfargument name=“personName” required=“true” type=“string” />

  4. <cfreturn “Your Name is “ & personName />

  5. </cffunction>

  6. </cfcomponent>

呼叫一个Webservice,在Flex和Javascript中是完全不同的方法。在Flex中,你只需要简单的使用标签即可。在标签中你可以定义”操作”来和Webservice关联。在这个示例中,我们将定义一个”getStuff”作为其中一个操作。我们将添加一个”onResult”函数到操作的返回事件上。这个处理返回的函数只是简单的将返回结果赋值给场景中的一个文本标签。

mxml:

  1. <!–

  2. WEB SERVICES

  3. –>

  4. <mx:WebService

  5. id=“sampleService”

  6. wsdl=“http://yourDomain/SoapTest.cfc?wsdl”>

  7. <mx:operation name=“getStuff” result=“onResult(event)” />

  8. </mx:WebService>

Actionscript:

  1. import mx.rpc.events.ResultEvent;

  2. private function callService(e:MouseEvent):void {

  3. sampleService.getStuff.send(myName.text);

  4. }

  5. private function onResult(e:ResultEvent):void {

  6. resultLabel.text = e.result as String;

  7. }

在JavaScript中,我们需要使用XMLHTTPRequest对象,就像我们在最后一个Tip所做的。基本上,我们将要添加一对自定义的Header,来传输SOAP数据包。

你可以参考”wsdl”文件来了解SOAP数据包。要参照wsdl文件,你只需在你的浏览器中敲入webservice文件的地址并加上”?wsdl”的后缀即可。wsdl描述了你的Webservice的细节。

参考资料:WSDL Tutorial

  1. var xmlhttp;

  2. var appXML;

  3. function callService() {

  4. var myName = document.getElementById(“myName”).value;

  5. var url = “http://yourDomain/SoapTest.cfc?wsdl”;

  6. xmlhttp = new XMLHttpRequest();

  7. xmlhttp.open(“POST”, url, true);

  8. xmlhttp.onreadystatechange=function(){

  9. if (xmlhttp.readyState==4) {

  10. var mainDiv = document.getElementById(‘result’);

  11. mainDiv.innerHTML = xmlhttp.responseText;

  12. }

  13. }

  14. xmlhttp.setRequestHeader(“Content-Type”, “text/xml”);

  15. xmlhttp.setRequestHeader(‘SOAPAction’,‘http://yourDomain/SoapTest.cfc?wsdl’);

  16. xmlhttp.send(“<?xml version=’1.0′ encoding=’UTF-8′?>”+\n\n+

  17. ‘<soapenv:Envelope’+

  18. ‘ xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”‘+

  19. ‘ xmlns:xsd=”http://www.w3.org/2001/XMLSchema”‘+

  20. ‘ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>’+

  21. ‘<soapenv:Body>’+

  22. ‘<ns1:getStuff xmlns:ns1=”http://communications”‘+

  23. ‘ soapenv:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/”>’+

  24. ‘<personName xsi:type=”xsd:string”>’ + myName + ‘</personName>’+

  25. ‘</ns1:getStuff>’+

  26. ‘</soapenv:Body>’+

  27. ‘</soapenv:Envelope>’);

  28. }

对于Flex来说,这是一个非常简单的示例,而在JavaScript中,你必须记住以下的要点:

  • 判断各种情况,确保你考虑到了所有的-一个小小的异常就会引发错误
  • 你可以将你的函数写在AIR之外,这样你就可以使用类似Firebug的工具来跟踪调试
  • 当呼叫一个Coldfunsion Webservice的时候,如果是一个需要RDS密码的页面,一定是你忘记了设置SOAPAction header
  • 在xmlhttp.open()函数中使用”POST”标签

Flex Example
Source Code

JavaScript Example
Source Code

Coldfusion Component
Source Code





上一篇:AIR读取/写入本地文件   下一篇:AIR:如何保存图片到本地
您的评论
  • 用户名:新注册) 密码: 匿名评论
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)

Copyright © 2006-2008 flashas.net All Rights Reserved.
网站内容咨询: admin#flashas.net (#为@) 联系QQ:40777822 浙ICP备06033001号
(本网站最佳浏览解析度为1024*768, 建议使用IE 6.0或以上版本浏览器。)