Search code examples
javaandroidweb-servicessoapksoap2

Android: How to get data from both header and body using KSOAP2


After following a few tutorials(pretty much all the Fahrenheit to Celsius tutorial) I am still at a loss as to how to actually obtain the data I require and set up the request properly. The main issue is getting inside the header and giving it the PartnerID inside <AccountInfo/>.

I also need to give it the following parameters in the body of the request:

<keyword>string</keyword>
<records>int</records>
<startingRecord>int</startingRecord>
<searchOptions>ID</searchOptions>

Can anyone explain how to achieve this?

POST /service/searchapi.asmx HTTP/1.1
Host: uk.company.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://api.company.com/service/SearchByKeyword"

<?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>
    <CompanyHeader xmlns="http://api.company.com/service">
      <AccountInfo>
        <PartnerID>string</PartnerID>
      </AccountInfo>
    </CompanyHeader>
  </soap:Header>
  <soap:Body>
    <SearchByKeyword xmlns="http://api.company.com/service">
      <keyword>string</keyword>
      <records>int</records>
      <startingRecord>int</startingRecord>
      <searchOptions>ID</searchOptions>
    </SearchByKeyword>
  </soap:Body>
</soap:Envelope>

Solution

  • Give this a go all you need to do is put in the correct values and then put it inside an AsyncTask or something like that. The first time i did soap i had a similar problem and it took me quite a while to figure it all out but fingers crossed this should work!

            Element[] header = new Element[1];
            header[0] = new Element().createElement(NAMESPACE, "CompanyHeader");
    
            Element accountInfo = new Element().createElement(NAMESPACE, "AccountInfo");
            header[0].addChild(Node.ELEMENT, accountInfo);
    
            Element apiKey = new Element().createElement(NAMESPACE, "PartnerID");
            apiKey.addChild(Node.TEXT, "String");
            accountInfo.addChild(Node.ELEMENT, apiKey);
    
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    
            PropertyInfo keyword =new PropertyInfo();
            keyword.name = "keyword";
            keyword.setValue("string");
            request.addProperty(keyword);
    
            PropertyInfo records =new PropertyInfo();
            records.name = "records";
            records.setValue(int);
            request.addProperty(records);
    
            PropertyInfo startingRecord =new PropertyInfo();
            startingRecord.name = "startingRecord";
            startingRecord.setValue(int);
            request.addProperty(startingRecord);
    
            PropertyInfo searchOptions =new PropertyInfo();
            searchOptions.name = "searchOptions";
            searchOptions.setValue(string);
            request.addProperty(searchOptions);
    
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope
                    .VER11);
            envelope.dotNet = true;
            envelope.implicitTypes = true;
            envelope.setAddAdornments(false);
            envelope.headerOut = header;
            envelope.bodyOut = request;
    
            HttpTransportSE ht = getHttpTransportSE();
    
            try {
                ht.call(SOAP_ACTION, envelope);
            } catch (HttpResponseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (envelope.getResponse() != null) {
                        SoapObject result = (SoapObject)envelope.getResponse();
                        Log.e("Results = ", String.valueOf(results));
                    }
                } catch (SoapFault e) {
                    e.printStackTrace();
                }
            }