flex webservice数据无法与flex数据网格绑定:Flash Builder 4.7



我正试图通过Web服务访问flex应用程序中SQLServer中可用的数据。

我有如下所示的wsdl文档:

<?xml version="1.0" encoding="UTF-8"?>
-<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> -<wsdl:types> -<s:schema targetNamespace="http://tempuri.org/" elementFormDefault="qualified"> -<s:element name="GetEmployees"> <s:complexType/> </s:element> -<s:element name="GetEmployeesResponse"> -<s:complexType> -<s:sequence> <s:element name="GetEmployeesResult" type="tns:ArrayOfEmployee" maxOccurs="1" minOccurs="0"/> </s:sequence> </s:complexType> </s:element> -<s:complexType name="ArrayOfEmployee"> -<s:sequence> <s:element name="Employee" type="tns:Employee" maxOccurs="unbounded" minOccurs="0" nillable="true"/> </s:sequence> </s:complexType> -<s:complexType name="Employee"> -<s:sequence> <s:element name="EmpId" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="EmpName" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="EmpEmail" type="s:string" maxOccurs="1" minOccurs="0"/> </s:sequence> </s:complexType> -<s:element name="SaveEmployee"> -<s:complexType> -<s:sequence> <s:element name="empId" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="empName" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="empEmail" type="s:string" maxOccurs="1" minOccurs="0"/> </s:sequence> </s:complexType> </s:element> -<s:element name="SaveEmployeeResponse"> <s:complexType/> </s:element> </s:schema> </wsdl:types> -<wsdl:message name="GetEmployeesSoapIn"> <wsdl:part name="parameters" element="tns:GetEmployees"/> </wsdl:message> -<wsdl:message name="GetEmployeesSoapOut"> <wsdl:part name="parameters" element="tns:GetEmployeesResponse"/> </wsdl:message> -<wsdl:message name="SaveEmployeeSoapIn"> <wsdl:part name="parameters" element="tns:SaveEmployee"/> </wsdl:message> -<wsdl:message name="SaveEmployeeSoapOut"> <wsdl:part name="parameters" element="tns:SaveEmployeeResponse"/> </wsdl:message> -<wsdl:portType name="ServiceSoap"> -<wsdl:operation name="GetEmployees"> <wsdl:input message="tns:GetEmployeesSoapIn"/> <wsdl:output message="tns:GetEmployeesSoapOut"/> </wsdl:operation> -<wsdl:operation name="SaveEmployee"> <wsdl:input message="tns:SaveEmployeeSoapIn"/> <wsdl:output message="tns:SaveEmployeeSoapOut"/> </wsdl:operation> </wsdl:portType> -<wsdl:binding name="ServiceSoap" type="tns:ServiceSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> -<wsdl:operation name="GetEmployees"> <soap:operation style="document" soapAction="http://tempuri.org/GetEmployees"/> -<wsdl:input> <soap:body use="literal"/> </wsdl:input> -<wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> -<wsdl:operation name="SaveEmployee"> <soap:operation style="document" soapAction="http://tempuri.org/SaveEmployee"/> -<wsdl:input> <soap:body use="literal"/> </wsdl:input> -<wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/> -<wsdl:operation name="GetEmployees"> <soap12:operation style="document" soapAction="http://tempuri.org/GetEmployees"/> -<wsdl:input> <soap12:body use="literal"/> </wsdl:input> -<wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> -<wsdl:operation name="SaveEmployee"> <soap12:operation style="document" soapAction="http://tempuri.org/SaveEmployee"/> -<wsdl:input> <soap12:body use="literal"/> </wsdl:input> -<wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:service name="Service"> -<wsdl:port name="ServiceSoap" binding="tns:ServiceSoap"> <soap:address location="http://localhost:28408/TestWebService/Service.asmx"/> </wsdl:port> -<wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12"> <soap12:address location="http://localhost:28408/TestWebService/Service.asmx"/> </wsdl:port> </wsdl:service> </wsdl:definitions>

我的mxml页面代码是:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView"
creationComplete="view1_creationCompleteHandler(event)">
<fx:Declarations>
<s:WebService id="ws" wsdl="http://localhost:28408/TestWebService/Service.asmx?wsdl" fault="fault(event)">
<s:operation name="GetEmployees" 
resultFormat="object"
result="GetEmployees(event)">
</s:operation>
<s:operation name="SaveEmployee" 
resultFormat="object"
result="SaveEmployee(event)">
</s:operation>
</s:WebService>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
[Bindable]
public var dataArray:ArrayCollection = new ArrayCollection();
protected function view1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
ws.GetEmployees();
}
private function GetEmployees(event:ResultEvent):void {
// Databind data from webservice to datagrid
dataArray = event.result;
}
private function SaveEmployee(event:ResultEvent):void {
// To Refresh DataGrid;
ws.GetEmployees(); 
//Alert.show("Saved Successfully");
}
private function AddRecord(event:MouseEvent):void {
// Save a record using a WebService method
//ws.SaveEmployee(txtEmpId.text, txtEmpName.text, txtEmpEmail.text); // 
}
private function fault(event:FaultEvent):void {
// Oppps some error occured
flt.text = event.fault.toString();
}
]]>
</fx:Script>
<s:DataGrid id="datagrid" dataProvider="{dataArray}" width="465" height="100%" visible="true">
<s:columns>
<s:ArrayList>
<s:GridColumn headerText="Emp Id" dataField="EmpId"/>
<s:GridColumn headerText="Emp Name" dataField="EmpName"/>
<s:GridColumn headerText="Emp Email" dataField="EmpEmail"/> 
</s:ArrayList>          
</s:columns>
</s:DataGrid>
<s:TextArea x="500" id="flt" visible="true" width="300"/>

当我试图运行这个项目时,我在将数据绑定到数据网格时出错,如下所示:1118:将静态类型为Object的值隐式强制为可能不相关的类型mx。collections:ArrayCollection.

我哪里错了?有人能告诉我吗?

您必须键入cast-event.result作为event.result as ArrayCollection。

喜欢私有函数GetEmployees(event:ResultEvent):void{dataArray=event.result as ArrayCollection;}

谨致问候,Vijay

您的event.result似乎不是ArrayCollection,而是一个普通对象。你将需要昆虫事件。结果并找出如何从中获得你的数组集合。

dataArray = event.result.someProperty;

dataArray = new ArrayCollection([event.result]); //if your service is just returning one item.

最新更新