0

I'm trying to get the response from a soap webservice using Ksoap2 but all i get is a string with the xml file inside, anyone know how I can parse each property?

here is the code I'm using:

 SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope soapEnvelope = new write(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        transport = new HttpTransportSE(URL);
        transport.debug=true;

        transport.call(SOAP_ACTION, soapEnvelope);
        response = (SoapObject) soapEnvelope.bodyIn;

And this is the Response I get:

Response{
     Result= <raiz>
               <result>
                 <exitoso>val</exitoso>
                   <message>message</message>
               </result>
               <clients>
                 <client>
                   <id>id</id>
                   <name>name</name>
                   <lastname>lastname</lastname>
                 </client>
               </clients>
             </raiz>; 
}

Anyone know how can I get the actual data? like the name, ID and last name?

CR-JC
  • 26
  • 6
  • What do you get when you traverse through your response and get the property value of the attribute? [SOAP Object > getProperty](http://kobjects.org/ksoap2/doc/api/org/ksoap2/serialization/SoapObject.html#getProperty(java.lang.String)) – veggirice Dec 11 '17 at 16:55
  • @veggirice If i try to navigate the response i get only the "Response" Property which is ` val message id name lastname ` Also my property count only returns 1 – CR-JC Dec 11 '17 at 17:15
  • A couple of questions: 1. What is the type of your response variable? Is it SoapObject? 2. If so, then when you do response.getProperty("name").toString(), what do you get? – veggirice Dec 11 '17 at 17:43
  • @veggirice I'm 95% sure it is a SoapObject, and if I try with the `response.getProperty("name").toString` I got the next Error `illegal property: name` – CR-JC Dec 11 '17 at 17:54
  • @veggirice even tho I can make a response.getProperty("Result") which returns the xml data : ` val message id name lastname ` – CR-JC Dec 11 '17 at 18:02

2 Answers2

0

Can you try this:

SoapObject rootNode = response.getProperty("Result");
for (int i=0; i<= rootNode.getPropertyCount(); i++) {
   SoapObject so = rootNode.getProperty(i);
   String name = so.getProperty("name").toString();
   //print name to log/console
}

Went by the documentation. You might need to resolve any casting or dependencies. What do you get for the name?

veggirice
  • 246
  • 1
  • 2
  • 15
  • If you aren't a 100% sure response is of type SoapObject, then just set : `SoapObject response = (SoapObject) soapEnvelope.bodyIn;` to the above snippet – veggirice Dec 11 '17 at 19:37
  • @veggierice I tried that but i got the following error: `org.ksoap2.serialization.SoapPrimitive cannot be cast to org.ksoap2.serialization.SoapObject` apparently Im dealing with a SoapPrimitive Response? – CR-JC Dec 11 '17 at 19:53
  • you might want to see this [link](https://stackoverflow.com/questions/16583796/when-to-use-soapobject-and-soapprimitive) and perhaps try to use SoapObject for your response from the envelope instead of SoapPrimitive – veggirice Dec 11 '17 at 19:57
  • @veggierice I'm using SoapObject for the response (I guess) like this response = (SoapObject) soapEnvelope.getResponse();, Sorry Im kinda new with Soap services but this is what they want to use at the office so I have to get into it :/ Also thanks for all the help – CR-JC Dec 11 '17 at 20:17
  • np. then it is complaining about soaprimitive somewhere – veggirice Dec 11 '17 at 21:03
  • I have noticed that the XML response from the web service returns this <![CDATA[ val message id name lastname ]]> could that be the problem? – CR-JC Dec 11 '17 at 21:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160972/discussion-between-jorch-cortez-and-veggirice). – CR-JC Dec 11 '17 at 21:33
0

Ok, so it took me a bit too much and I didn't find an other way that actually worked for me so here is what i did.

first of all I call the web service which returns a CDATA with an xml document as a string like this:

<![CDATA[<users>
     <user>
        <username>myusername</username>
        <password>myPassword</password>
     </user>
</users>]]>

Once I get the response I created a class User:

public class user
{
    public String userName, password;
}

And i have a class get_values

public class get_values {

public static Document doc;
//--- Returns an ArrayList with the values of the User using the User Class
public static ArrayList<user> UserParser(SoapObject response)
        {
            XmlPullParserFactory parserFactory;
            try{
                parserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = parserFactory.newPullParser();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Source xmlSource = new DOMSource(GetDocFromString(response.getProperty(0).toString()));
                Result outputTarget = new StreamResult(outputStream);
                TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
                InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(is, null);

                return UserProcessParser(parser);

            } catch (XmlPullParserException e) {

            } catch (IOException e) {
            } catch (TransformerConfigurationException e) {
            } catch (TransformerException e) {
                e.printStackTrace();
            }
            return null;
        }


        //--- Creates an xml document using the web service's response
        private static Document GetDocFromString(String xmlStr)
        {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            try
            {
                builder = factory.newDocumentBuilder();
                doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );
                return doc;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        //--- Returns the xml values as an ArrayList
        private static ArrayList<user> UserProcessParser(XmlPullParser parser) throws XmlPullParserException, IOException
        {
            ArrayList<user> user_data = new ArrayList<>();
            int eventType = parser.getEventType();
            user_data user_returned = null;

            while (eventType != XmlPullParser.END_DOCUMENT){
                String elementName = null;
                switch (eventType){
                    case XmlPullParser.START_TAG:
                        elementName = parser.getName();
                        if ("user".equals(elementName))
                        {
                            user_returned = new user_data();
                            user_data.add(user_returned);
                        }
                        else if (user_returned != null)
                        {
                            if ("username".equals(elementName))
                            {
                                user_returned.username = parser.nextText();
                            }
                            else if ("password".equals(elementName))
                            {
                                user_returned.password = parser.nextText();
                            }
                        }
                        break;
                }
                eventType = parser.next();
            }

            return user;
        }

    }

then I just call the function to get the arraylist with all the values:

ArrayList<user> user_data = get_values.UserParser(MY_WEBSERVICE_RESPONSE);

And I get all the values I need, then in case of needing it for any other web service i Add another Parser function and another process parser function, then if it returns null you just have to check the response.

Well that's the solution I ended up with I hope it can help anyone else

CR-JC
  • 26
  • 6