Search code examples
android.netwcfsoapksoap2

Changing the layout of a SOAP envelope with Ksoap2 for android


I'm working on a project to connect an Android powered device to a .NET wcf server.

The problem I'm having is with the layout of the SOAP envelope. When I'm using soapUI to send a request, the request is handled properly. However, when I use Ksoap2 for Android I get a null value in return within a SOAP return envelope, the server logs there has been a request with HTTP code =200(OK).

How can I setup KSOAP2 that it creates the same SOAP envelope as is used in soapUI?

Here is a working request using soapUI. This is how the layout should look.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
    <tem:GetPerson>
      <tem:name>Roel</tem:name>
    </tem:GetPerson>
  </soapenv:Body>
</soapenv:Envelope>

And return with soapUI

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
  <GetPersonResponse xmlns="http://tempuri.org/">
     <GetPersonResult xmlns:a="http://schemas.datacontract.org/2004/07/StageService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Age>21</a:Age>
        <a:Name>Roel</a:Name>
     </GetPersonResult>
  </GetPersonResponse>
 </s:Body>
</s:Envelope>

I have made a Dump of the soap envelope. This is how the layout looks now when I send it from the Android application.

<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>
    <GetPerson xmlns="http://tempuri.org/" id="o0" c:root="1">
        <n0:name i:type="d:string" xmlns:n0="tem">Roel</n0:name>
    </GetPerson>
</v:Body>
</v:Envelope>

And the return envelope of this request

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <GetPersonResponse xmlns="http://tempuri.org/">
        <GetPersonResult i:nil="true" xmlns:a="http://schemas.datacontract.org/2004/07/StageService1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>
    </GetPersonResponse>
</s:Body>
</s:Envelope>

Could anyone explain how I get the output layout to be the same as the one I send from soapUI?

My code from the complete application:

private static final String SOAP_ACTION = "http://tempuri.org/IService1/GetPerson";
private static final String METHOD_NAME = "GetPerson";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL =      "http://192.168.4.231/TestWebservice/Service1.svc?wsdl";

try {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    Log.e("debug","Creating new soap object");

    PropertyInfo pi = new PropertyInfo();
    pi.setNamespace("tem");
    pi.setName("name");
    pi.setValue("Roel");
    request.addProperty(pi);

    SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
    Log.e("debug","Creating new soap envelope");
    envelope.dotNet=true;

    envelope.setOutputSoapObject(request);
    Log.e("debug","setoutput soap object");

    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.13.8", 8080));

    HttpTransportSE androidHttpTransport = new HttpTransportSE(proxy, URL);
    Log.e("debug","new http transport init");

    androidHttpTransport.debug = true;

    androidHttpTransport.call(SOAP_ACTION, envelope);
    Log.e("debug","http transport call");
    System.out.println(androidHttpTransport.requestDump);
    System.out.println(androidHttpTransport.responseDump);

    SoapObject result = (SoapObject)envelope.getResponse();
    Log.e("debug","get response");

    tv.setText( ""+result);

} catch (Exception e) {
    tv.setText(e.getMessage());
    Log.e("error","somthing went wrong!!");
    e.toString();
    System.out.println(e);
}

Thanks in advance,

Fabian


Solution

  • PropertyInfo pi = new PropertyInfo();
    pi.setNamespace("tem");
    pi.setName("name");
    pi.setValue("Roel");
    request.addProperty(pi);
    

    The Namespace should be the URL of the Namespace, not the variable. So:

    pi.setNamespace("http://tempuri.org/");