如何使用kso​​ap2解析复杂的对象?

时间:2018-11-02 02:03:18

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

我正在使用Android Studio和ksoap2(版本3.6.2)连接到Soap Web服务,但我得到了SoapObject作为回报,但如预期的那样。我在为xml请求中的嵌套属性创建代码时遇到问题。即使向请求添加属性,Web服务也始终会发送相同的响应(.xml请求在SoapUI上可以正常工作,并且Webservice发送正确的响应)。

这是我需要从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);

但是Web服务总是发送响应,就像我应该在请求上发送响应一样:

<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> 

请,我们将不胜感激。预先感谢!

注意:我已经检查了请求中的名称空间,MethodName和URL。

请帮助。

1 个答案:

答案 0 :(得分:0)

经过大量研究,我找到了答案。我正在使用SAP Web服务和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 。所以我不得不使用此类在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是Webservice期望的XML。我仍然不知道为什么SAP Web服务仍然使用SOAP而不是Restful服务。我们现在处于2018年,这种Web服务看起来有些陈旧。