Shark
Shark

Reputation: 165

Expecting Nested XML structure but receiving SOAP structure

I'm making an app that is requesting data from a Web Service (implementing Soap).

People who are viewing this pleaase post doubts in comments... I havent got any response to the question do ask me if there is any doubts, i really need help, am stuck!!

So to make request I have to use ksoap libraries.. the Web service is coded to return a response of type XML. When the web service itself is tested on a browser it displays a result which is as follows:

?xml version="1.0" encoding="utf-8" ?> 
- <SOBKeyList>
- <Key>
  <value>12686</value> 
  </Key>
- <Key>
  <value>16238</value> 
  </Key>
- <Key>
  <value>26978</value> 
  </Key>
  </SOBKeyList>

clearly an XML file...

However when i use ths code to get a result:

    public String getXmlFromUrl(String url) {
    // TODO Auto-generated method stub
    String xml = null;
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
    PropertyInfo pi = new PropertyInfo();

    pi.setName("fkey");
    pi.setValue(0);
    pi.setType(Integer.class);
    request.addProperty(pi);

    pi = new PropertyInfo();

    pi.setName("tkey");
    pi.setValue(999999);
    pi.setType(Integer.class);
    request.addProperty(pi);


    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransport = new HttpTransportSE(url);
    Object response = null; 

    try {
        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();
        xml = response.toString();
        Log.d("xml:", xml);
    } catch (SoapFault e) {
        // TODO Auto-generated catch block
        Log.d("Soap Fault", e.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("IOexception", e.toString());
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        Log.d("XmlPullParserException", e.toString());
    }

    return xml;
}

It returns a nested SOAP structure confirmed by the Log entry which i make ( Log.d("xml:", xml); ) The corresponding LogCat entry is: (i've formatted it to make it the SOAP structure's heirarchy apparent... )

anyType{
      SOBKeyList=anyType{
                          Key=anyType{value=12686 ; };
                          Key=anyType{value=16238 ; };
                          Key=anyType{value=26978 ; };
                         };
          }

The reason why i necessarily need an XML is because later I parse the string to get a DOM element and when the above string is passed it returns the following:

org.xml.sax.SAXParseException: Unexpected token (position:TEXT anyType{SOBKeyLi...@1:119 in java.io.StringReader@40ec9c68)

also from there onward my entire code depends on the fact that the response was XML.

Explaination of the reason why i expected an XML : Now you may asked why I coded my app expecting XML when i had not tested the Web service the reason is: The Web service was coded my a third party who assured me of an XML response , now i dont have enough time to change my remaining code to exploit a SOAP structure !! -_- I'm in a fix. Please help!

Upvotes: 0

Views: 1357

Answers (1)

Oguz &#214;zkeroglu
Oguz &#214;zkeroglu

Reputation: 3057

Try this:

String responseXML = httpTransport.responseDump;
  • HttpTransportSE.responseDump gives you response in XML format.
  • HttpTransportSE.requestDump gives you request in XML format.

However to be able to change and retrieve any of these values you must set the debug field of HttpTransportSE to true so your code should look like...

    HttpTransportSE httpTransport = new HttpTransportSE(url);
    httpTransport.debug =true;      
    try {
        httpTransport.call(SOAP_ACTION, envelope);          
        xml = httpTransport.responseDump;
        Log.d("xml:", "is:" + xml);     
    }

Also you can parse your response like as follow:

ArrayList<String> listValues = new ArrayList<String>();

            httpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject) envelope.getResponse();

            SoapObject soapSOBKeyList = (SoapObject) response.getProperty("SOBKeyList");

            int keyCount = soapSOBKeyList.getPropertyCount();

            for (int i = 0; i < keyCount; i++) {
                String value = soapSOBKeyList.getPropertyAsString(i);
                listValues.add(value);
            }

            return listValues;

Upvotes: 1

Related Questions