2010-09-02 75 views
0

從使用KSOAP向J2ME客戶端發送複雜對象數組到.NET Web服務(Soap服務)時,我遇到了一個問題。使用kso​​ap2向.NET Web服務發送複雜對象數組

我TestSoap.java文件具有setArr()方法,該方法採用PropHeaderMapBin對象的陣列,並且將其發送到其返回一個整數(PropHeaderMapBin對象的計數)setArr的

TestSoap.java 

package org.tempuri; 

    import com.n9ciphers.test.Constants; 
    import java.util.Vector; 
    import org.ksoap2.SoapEnvelope; 
    import org.ksoap2.serialization.PropertyInfo; 
    import org.ksoap2.serialization.SoapObject; 
    import org.ksoap2.serialization.SoapSerializationEnvelope; 
    import org.ksoap2.transport.HttpTransport; 
    import org.tempuri.PropHeaderMapBin; 

    public final class TestSoap { 

     public int setArr(PropHeaderMapBin[] inArr) throws Exception { 

      SoapObject _client = new SoapObject("http://tempuri.org/", "SetArr"); 
      SoapObject[] _propHeaderMapBinSO = new SoapObject[inArr.length]; 

      PropHeaderMapBin[] prop = inArr; 
      //PropHeaderMapBin[] prop = new PropHeaderMapBin[inArr.length]; 
      Vector _propVector = new Vector(inArr.length); 
      if (inArr != null) { 

       //System.out.println("_len " + inArr.length); 

       for (int i = 0; i < inArr.length; i++) { 
        _propHeaderMapBinSO[i] = new SoapObject("http://tempuri.org/", "PropHeaderMapBin"); 
        _propHeaderMapBinSO[i].addProperty("PropertyName", prop[i].getPropertyName()); 
        _propHeaderMapBinSO[i].addProperty("value", prop[i].getValue()); 
        _propHeaderMapBinSO[i].addProperty("HeaderText", prop[i].getHeaderText()); 
        _propHeaderMapBinSO[i].addProperty("label", prop[i].getLabel()); 

        _propVector.addElement(_propHeaderMapBinSO[i]); 
       } 
      } 

      _client.addProperty(pi, _propVector); 

      SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      _envelope.addMapping("http://www.tempuri.org/", "PropHeaderMapBin", new PropHeaderMapBin().getClass()); 
      //_envelope.addTemplate (_client); 
      _envelope.bodyOut = _client; 
      _envelope.dotNet = true; 

      HttpTransport _ht = new HttpTransport(Constants.endPoint + "Test.asmx"); 
      _ht.debug = true; 
      _ht.call("http://tempuri.org/SetArr", _envelope); 

      //System.out.println("response: " + result.toString()); 

      System.out.println("Request Dump:" + _ht.requestDump); 
      //Dialog.show("Request Dump", _ht.requestDump, Dialog.TYPE_INFO, null, "OK", null); 
      System.out.println("Response Dump:" + _ht.responseDump); 
      //Dialog.show("Response Dump", _ht.responseDump, Dialog.TYPE_INFO, null, "OK", null); 

      return Integer.parseInt(_envelope.getResponse().toString()); 
      //return 0; 
     } 
    } 

輸出的web服務( )方法調用。的服務器不能識別在陣列的對象的數據類型,並將其設置到數組類型ANYTYPE [3],並返回0而不是3.

Request Dump: 
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"> 
    <v:Header /> 
    <v:Body> 
     <SetArr xmlns="http://tempuri.org/" id="o0" c:root="1"> 
      <inArr i:type="c:Array" c:arrayType="d:anyType[3]"> 
       <item i:type="n0:PropHeaderMapBin" xmlns:n0="http://tempuri.org/"> 
        <PropertyName i:type="d:string">1</PropertyName> 
        <value i:type="d:string">1</value> 
        <HeaderText i:type="d:string">Pradhan</HeaderText> 
        <label i:type="d:string">Pradhan</label> 
       </item> 
       <item i:type="n1:PropHeaderMapBin" xmlns:n1="http://tempuri.org/"> 
        <PropertyName i:type="d:string">2</PropertyName> 
        <value i:type="d:string">2</value> 
        <HeaderText i:type="d:string">Kavya</HeaderText> 
        <label i:type="d:string">Kavya</label> 
       </item> 
       <item i:type="n2:PropHeaderMapBin" xmlns:n2="http://tempuri.org/"> 
        <PropertyName i:type="d:string">3</PropertyName> 
        <value i:type="d:string">3</value> 
        <HeaderText i:type="d:string">Puchi</HeaderText> 
        <label i:type="d:string">Puchi</label> 
       </item> 
      </inArr> 
     </SetArr> 
    </v:Body> 
</v:Envelope> 


Response Dump:<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <SetArrResponse xmlns="http://tempuri.org/"> 
      <SetArrResult>0</SetArrResult> 
     </SetArrResponse> 
    </soap:Body> 
</soap:Envelope> 
response = 0 

我的WSDL文件以供參考

<wsdl:definitions targetNamespace="http://tempuri.org/"> 
    <wsdl:types> 
     <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> 
      <s:element name="GetArr"> 
       <s:complexType/> 
      </s:element> 
      <s:element name="GetArrResponse"> 
       <s:complexType> 
        <s:sequence> 
         <s:element minOccurs="0" maxOccurs="1" name="GetArrResult" type="tns:ArrayOfPropHeaderMapBin"/> 
        </s:sequence> 
       </s:complexType> 
      </s:element> 
      <s:complexType name="ArrayOfPropHeaderMapBin"> 
       <s:sequence> 
        <s:element minOccurs="0" maxOccurs="unbounded" name="PropHeaderMapBin" nillable="true" type="tns:PropHeaderMapBin"/> 
       </s:sequence> 
      </s:complexType> 
      <s:complexType name="PropHeaderMapBin"> 
       <s:sequence> 
        <s:element minOccurs="0" maxOccurs="1" name="PropertyName" type="s:string"/> 
        <s:element minOccurs="0" maxOccurs="1" name="value" type="s:string"/> 
        <s:element minOccurs="0" maxOccurs="1" name="HeaderText" type="s:string"/> 
        <s:element minOccurs="0" maxOccurs="1" name="label" type="s:string"/> 
       </s:sequence> 
      </s:complexType> 
      <s:element name="SetArr"> 
       <s:complexType> 
        <s:sequence> 
         <s:element minOccurs="0" maxOccurs="1" name="inArr" type="tns:ArrayOfPropHeaderMapBin"/> 
        </s:sequence> 
       </s:complexType> 
      </s:element> 
      <s:element name="SetArrResponse"> 
       <s:complexType> 
        <s:sequence> 
         <s:element minOccurs="1" maxOccurs="1" name="SetArrResult" type="s:int"/> 
        </s:sequence> 
       </s:complexType> 
      </s:element> 
     </s:schema> 
    </wsdl:types> 
    <wsdl:message name="GetArrSoapIn"> 
     <wsdl:part name="parameters" element="tns:GetArr"/> 
    </wsdl:message> 
    <wsdl:message name="GetArrSoapOut"> 
     <wsdl:part name="parameters" element="tns:GetArrResponse"/> 
    </wsdl:message> 
    <wsdl:message name="SetArrSoapIn"> 
     <wsdl:part name="parameters" element="tns:SetArr"/> 
    </wsdl:message> 
    <wsdl:message name="SetArrSoapOut"> 
     <wsdl:part name="parameters" element="tns:SetArrResponse"/> 
    </wsdl:message> 
    <wsdl:portType name="TestSoap"> 
     <wsdl:operation name="GetArr"> 
      <wsdl:input message="tns:GetArrSoapIn"/> 
      <wsdl:output message="tns:GetArrSoapOut"/> 
     </wsdl:operation> 
     <wsdl:operation name="SetArr"> 
      <wsdl:input message="tns:SetArrSoapIn"/> 
      <wsdl:output message="tns:SetArrSoapOut"/> 
     </wsdl:operation> 
    </wsdl:portType> 
    <wsdl:binding name="TestSoap" type="tns:TestSoap"> 
     <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> 
     <wsdl:operation name="HelloWorld"> 
      <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document"/> 
      <wsdl:input> 
       <soap:body use="literal"/> 
      </wsdl:input> 
      <wsdl:output> 
       <soap:body use="literal"/> 
      </wsdl:output> 
     </wsdl:operation> 
     <wsdl:operation name="GetArr"> 
      <soap:operation soapAction="http://tempuri.org/GetArr" style="document"/> 
      <wsdl:input> 
       <soap:body use="literal"/> 
      </wsdl:input> 
      <wsdl:output> 
       <soap:body use="literal"/> 
      </wsdl:output> 
     </wsdl:operation> 
     <wsdl:operation name="SetArr"> 
      <soap:operation soapAction="http://tempuri.org/SetArr" style="document"/> 
      <wsdl:input> 
       <soap:body use="literal"/> 
      </wsdl:input> 
      <wsdl:output> 
       <soap:body use="literal"/> 
      </wsdl:output> 
     </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:binding name="TestSoap12" type="tns:TestSoap"> 
     <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/> 
     <wsdl:operation name="GetArr"> 
      <soap12:operation soapAction="http://tempuri.org/GetArr" style="document"/> 
      <wsdl:input> 
       <soap12:body use="literal"/> 
      </wsdl:input> 
      <wsdl:output> 
       <soap12:body use="literal"/> 
      </wsdl:output> 
     </wsdl:operation> 
     <wsdl:operation name="SetArr"> 
      <soap12:operation soapAction="http://tempuri.org/SetArr" style="document"/> 
      <wsdl:input> 
       <soap12:body use="literal"/> 
      </wsdl:input> 
      <wsdl:output> 
       <soap12:body use="literal"/> 
      </wsdl:output> 
     </wsdl:operation> 
    </wsdl:binding> 
    <wsdl:service name="Test"> 
     <wsdl:port name="TestSoap" binding="tns:TestSoap"> 
      <soap:address location="http://xxx.xxx.xxx.xxx/Services/Test.asmx"/> 
     </wsdl:port> 
     <wsdl:port name="TestSoap12" binding="tns:TestSoap12"> 
      <soap12:address location="http://xxx.xxx.xxx.xxx/Services/Test.asmx"/> 
     </wsdl:port> 
    </wsdl:service> 
</wsdl:definitions> 

我如果有人已經面對這樣的問題併成功地解決了這個問題,那麼武漢市就會很感激。

+0

user435696:您是否找到解決此問題的解決方案?我有同樣的問題。你的信息幫助了我很多。 – 2011-02-01 13:39:54

+0

這是什麼解決方案?我嘗試了soapUI,但似乎無法發現ksoap2和s soapUI請求之間的任何重大差異 – AhmedW 2011-02-01 13:40:02

回答

1

我可以給你提示解決問題。

使用soapUI工具創建具有相同WSDL的Web服務項目(它非常容易和快速)。

將您的請求轉儲與soapUI生成的soap body進行比較。你會發現差異和

因此的解決方案。

0

@Rajnikant有它現貨。如果您有無法使用的Web服務,請使用SOAPui導入wsdl端點。這將爲您可以用來調用您的wcf方法並查看請求轉儲的方法創建一個測試工具。將此與來自移動設備的請求轉儲(使用androidHttpTransport.requestDump)進行比較.....請注意傳遞的參數及其命名空間....這非常重要

相關問題