Search code examples
androidksoap2android-ksoap2

ksoap2: Post a soap request according to this request XML


My soap request looks like this

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <Authenticator xmlns="http://www.namespacename.com/services/">
      <UserName>string</UserName>
      <Password>string</Password>
    </Authenticator>
  </soap:Header>
  <soap:Body>
    <ListItems xmlns="http://www.namespacename.com/services/">
      <strCode>string</strCode>
    </ListItems>
  </soap:Body>
</soap:Envelope>

I'm at least successfully able to login using ksoap2 but I haven't figured out about the body part, any ideas how to call ListItems with parameter strCode, like it shows above.

Here's the current code.

     String NAMESPACE = "http://www.namespace.com/services/";
     String METHOD_NAME = "ListItems";
     String SOAP_ACTION = "http://www.namespace.com/services/ListItems";
     String URL = "https://www.kupong.se/Services/CouponAPI18.asmx";

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              // Enable the below property if consuming .Net service
      envelope.dotNet = true;
      envelope.setOutputSoapObject(request);

      envelope.headerOut = new Element[1];
      envelope.headerOut[0] = buildAuthHeader();

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
          try 
          {
              androidHttpTransport.call(SOAP_ACTION, envelope, null);
              response = (SoapObject)envelope.getResponse();

          }
          catch(Exception e)
          {
              e.printStackTrace();
          }     

function for preparing header values for authentication

private Element buildAuthHeader() {
            Element h = new Element().createElement(NAMESPACE, "Authenticator");

            Element username = new Element().createElement(NAMESPACE, "UserName");
            username.addChild(Node.TEXT, username);
            h.addChild(Node.ELEMENT, username);
            Element pass = new Element().createElement(NAMESPACE, "Password");
            pass.addChild(Node.TEXT, "password)
            h.addChild(Node.ELEMENT, pass);

            return h;
    }

Solution

  • I was just looking for a way to post body message.

    All you need is to create a SoapObject and pass it to bodyOut property of envelope.

          SoapObject sub = new SoapObject(NAMESPACE, METHOD);
          sub.addProperty("strCode", value);
          envelope.bodyOut = sub;