【问题标题】:Passing XML(DataSet) as Parameter ksoap2 android将 XML(DataSet) 作为参数传递 ksoap2 android
【发布时间】:2015-01-25 16:22:25
【问题描述】:

我正在尝试使用 ksop2 将 XML 请求发送到 Web 服务 但它不工作

我的网络服务请求格式是

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <UpdateVehicleViaObj xmlns="http://tempuri.org/">
            <userHash>[string?]</userHash>
            <vehicleObject>
                <Colour xmlns="http://schemas.datacontract.org/2004/07/StockService">[string?]</Colour>
                <Comments xmlns="http://schemas.datacontract.org/2004/07/StockService">[string?]</Comments>
                <Condition xmlns="http://schemas.datacontract.org/2004/07/StockService">[string?]</Condition>                
            </vehicleObject>
        </UpdateVehicleViaObj>
    </Body>
</Envelope>

我正在使用 ksoap2 来创建类似的请求

SoapObject request = new SoapObject("Namespace", "methodname");
  request.addProperty(properyObject);

 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        //SOAP is implemented in dotNet true/false.
        envelope.dotNet = true;
        MarshalDouble md = new MarshalDouble();
        //envelope.implicitTypes = true;
        envelope.implicitTypes = true;
        md.register(envelope);
        //Set request data into envelope and send request using HttpTransport
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(mInObj.getUrl(), networkTimeOut);

        androidHttpTransport.debug= true;
        androidHttpTransport.call(SoapAction, envelope,headerPropertyArrayList);

和ksop2使requst变成这样

<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><UpdateVehicleViaObj xmlns="http://tempuri.org/" id="o0" c:root="1"><userHash>B5B2FDF87E848946</userHash><vehicleObject>&lt;Colour&gt;red&lt;/Colour&gt;&lt;
&lt;Comments &gt;red&lt;/Comments &gt;&lt;&lt;Condition &gt;red&lt;/Condition &gt;&lt;</vehicleObject></UpdateVehicleViaObj></v:Body></v:Envelope>

请帮忙..

【问题讨论】:

    标签: android xml web-services wsdl android-ksoap2


    【解决方案1】:

    看ksoap2的文档

    https://code.google.com/p/ksoap2-android/wiki/CodingTipsAndTricks#sending/receiving_array_of_complex_types_or_primitives

    您可以创建将实现可编组接口的类并在该类中添加其他属性

    【讨论】:

      【解决方案2】:

      可能有点晚了,但我刚刚解决了它实现 KvmSerializable 类,并且在该类中,CDATA 字符串被声明为 SoapObject。然后,我用 SoapObject.setInnerText 设置 CDATA

              SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
      
      
              SoapObject fieldWithCdata = new SoapObject(NAMESPACE, "fieldWithCData");
              fieldWithCdata.setInnerText(cDatraString);
      
              CustomClass customClass = new customClass(string1, string2, string3, fieldWithCdata);
      
      
              PropertyInfo pi = new PropertyInfo();
              pi.setNamespace(NAMESPACE);
              pi.setName("customClass");
              pi.setValue(customClass);
      
              request.addProperty(pi);
      

      在自定义类中

      public class CustomClass implements KvmSerializable{
      
      
      private String string1;
      private String string2;
      private String string3;
      private SoapObject fieldWithCdata;
      

      ...

       public void setProperty(int index, Object value) {
          switch (index)
          {
              case INDEX_STRING1: //0
                  this.string1 = value.toString();
                  break;
              case INDEX_STRING2: //1
                  this.string2 = value.toString();
                  break;
              case INDEX_STRING3: //2
                  this.string3 = value.toString();
                  break;
              case INDEX_FIELDWITHCDATA://3
                  this.fieldWithCdata = (SoapObject) value;
                  break;
          }
      

        public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
          switch(index){
              case INDEX_STRING1:
                  info.name = "string1";
                  info.type = PropertyInfo.STRING_CLASS;
                  break;
              case INDEX_STRING2:
                  info.name = "string2";
                  info.type = PropertyInfo.STRING_CLASS;
                  break;
              case INDEX_STRING3:
                  info.name = "string3";
                  info.type = PropertyInfo.STRING_CLASS;
                  break;
              case INDEX_FIELDWITHCDATA:
                  info.name = "fieldWithCdata";
                  info.type = PropertyInfo.OBJECT_TYPE;
                  break;
              default:
                  break;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 2018-07-06
        • 2015-01-11
        相关资源
        最近更新 更多