Reputation: 11
I'm using Android Studio and ksoap2 (version 3.6.2) to connect to a soap web service and i get a SoapObject in return but not as expected. I'm having problems creating the code for the nested properties inside the xml request. The webservice always send the same response even adding properties to the request (The .xml request works fine on SoapUI and the webservice sends the correct response).
Here is the .xml that i need to send from Android:
<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>
And here is the code in 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);
But the webservice always send the response as if i should be sending this on the request:
<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>
Please, any help would be very appreciated. Thanks in advance!
Note: I have already checked the Namespace, MethodName and URL in the request.
Please, help.
Upvotes: 1
Views: 442
Reputation: 11
After a lot of research i found the answer. I am working with a SAP webservice and Android Handhelds. To achieve the xml request with ksoap2 i had to use this to create the 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;
But it seems that the ksoap2 serialization use the http://www.w3.org/2001/XMLSchema-instance eschema by default. So i had to serialize on the SAP way using this class:
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();
}
}
And thats it. The xml is the expected one by the webservice. I still don´t know why the SAP webservices are still using SOAP instead of Restful services. We are in 2018 and this kind of webservices looks a little bit old.
Upvotes: 0