【问题标题】:How to parse a complex object with ksoap2?如何用 ksoap2 解析复杂对象?
【发布时间】:2018-11-02 02:03:17
【问题描述】:

我正在使用 Android Studio 和 ksoap2(版本 3.6.2)连接到一个肥皂网络服务,我得到一个 SoapObject 作为回报,但 不是 符合预期。我在为 xml 请求中的嵌套属性创建代码时遇到问题。即使向请求添加属性,Web 服务也始终发送相同的响应(.xml 请求在 SoapUI 上运行良好,并且 Web 服务发送正确的响应)。

这是我需要从 Android 发送的 .xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org    /soap/envelope/" xmlns:glob="http://abc.def">
   <soapenv:Header/>
   <soapenv:Body>
      <glob:MaterialByElements>
        <MaterialSelectionByElements>
          <SelectionByID>
            <Code>I</Code>
            <TypeCode>1</TypeCode>
            <ID>IM-640</ID>
          </SelectionByID>
        </MaterialSelectionByElements>         
      </glob:MaterialByElements>
   </soapenv:Body>
</soapenv:Envelope> 

这是Android中的代码:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

SoapObject SelectionByID = new SoapObject(NAMESPACE, "SelectionByID");
SelectionByID .addProperty("Code", "I");
SelectionByID .addProperty("TypeCode", "1");
SelectionByID .addProperty("ID", "IM-640-1045");

SoapObject MaterialSelectionByElements = new SoapObject(NAMESPACE,"MaterialSelectionByElements");

MaterialSelectionByElements.addSoapObject(SelectionByID);
request.addSoapObject(MaterialSelectionByElements);

但是网络服务总是发送响应,就好像我应该在请求中发送它一样:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org    /soap/envelope/" xmlns:glob="http://abc.def">
   <soapenv:Header/>
   <soapenv:Body>
      <glob:MaterialByElements>        
       <MaterialSelectionByElements>

       </MaterialSelectionByElements>         
      </glob:MaterialByElements>
   </soapenv:Body>
</soapenv:Envelope> 

请,任何帮助将不胜感激。提前致谢!

注意:我已经检查了请求中的 Namespace、MethodName 和 URL。

请帮忙。

【问题讨论】:

    标签: android-studio soap xml-parsing android-ksoap2 ksoap


    【解决方案1】:

    经过大量研究,我找到了答案。我正在使用 SAP 网络服务和 Android 手持设备。要使用 ksoap2 实现 xml 请求,我必须使用它来创建 SoapObject:

            SoapObject request = null;
            SoapObject SelectionByID = new SoapObject(null, "SelectionByID");
            SelectionByID.addProperty("Code", "I");
            SelectionByID.addProperty("TypeCode", "1");
            SelectionByID.addProperty("ID", "IM-640");
            SoapObject MaterialSelectionByElements = new SoapObject("", "MaterialSelectionByElements");
            MaterialSelectionByElements.addSoapObject(SelectionByID);
            request=MaterialSelectionByElements;
    

    但似乎 ksoap2 序列化使用 http://www.w3.org/2001/XMLSchema-instance 默认为 eschema。所以我不得不使用这个类以 SAP 方式进行序列化:

    public static class SAPSerializationEnvelope extends SoapSerializationEnvelope {
    
        public String namespace;
        public SAPSerializationEnvelope(int version, String namespace) {
            super(version);
            this.namespace= namespace;
        }
    
        @Override
        public void write(XmlSerializer writer) throws IOException {
    
            writer.setPrefix("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
            writer.setPrefix("glob", namespace);
            writer.setPrefix("soapenv", env);
            writer.startTag(env, "Envelope");
            writer.startTag(env, "Header");
            writeHeader(writer);
            writer.endTag(env, "Header");
            writer.startTag(env, "Body");
            writer.setPrefix("glob", namespace);
            writer.startTag(namespace, "MaterialByElementsQuery_sync");
            writeBody(writer);
            writer.endTag(namespace, "MaterialByElementsQuery_sync");
            writer.endTag(env, "Body");
            writer.endTag(env, "Envelope");
    
        }
    
        @Override
        public Object getResponse() throws SoapFault {
            return super.getResponse();
        }
    
    }
    

    就是这样。 xml 是 web 服务所期望的。我仍然不知道为什么 SAP 网络服务仍在使用 SOAP 而不是 Restful 服务。我们是 2018 年,这种网络服务看起来有点老了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多