Skip to content Skip to sidebar Skip to footer

How To Do The Parsing Of Soap Response

I am new in android development.I am getting the SOAP response but I do not know that how to do the parsing of values of the SOAP Response. Actually I am trying to parse the String

Solution 1:

Try this method and check if it works

publicvoid Parser(){

        classTopGoalScores{
            String Emotion;
            int Color;
            String EmotionId;
            String IsHappyEmotion;

        }

        TopGoalScores topGoalScores=new TopGoalScores();

         try {
            SoapParser.parseBusinessObject(response.getProperty(0).toString(), topGoalScores);

         } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }


    }

This is Soap Parser Class

import java.lang.reflect.Field;
import java.lang.reflect.Type;

publicclassSoapParser {

    /**
     * Parses a single business object containing primitive types from the
     * response
     * 
     * @param input
     *            soap message, one element at a time
     * @param theClass
     *            your class object, that contains the same member names and
     *            types for the response soap object
     * @return the values parsed
     * @throws NumberFormatException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */publicstaticvoidparseBusinessObject(String input, Object output)throws NumberFormatException, IllegalArgumentException,
            IllegalAccessException, InstantiationException {

        ClasstheClass= output.getClass();
        Field[] fields = theClass.getDeclaredFields();

        for (inti=0; i < fields.length; i++) {
            Typetype= fields[i].getType();
            fields[i].setAccessible(true);

            // detect Stringif (fields[i].getType().equals(String.class)) {
                Stringtag="s" + fields[i].getName() + "="; // "s" is for// String in the// above soap// response// example +// field name// for example// Name =// "sName"if (input.contains(tag)) {
                    StringstrValue= input.substring(
                            input.indexOf(tag) + tag.length(),
                            input.indexOf(";", input.indexOf(tag)));
                    if (strValue.length() != 0) {
                        fields[i].set(output, strValue);
                    }
                }
            }

            // detect int or Integerif (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
                Stringtag="i" + fields[i].getName() + "="; // "i" is for// Integer or// int in the// above soap// response// example+// field name// for example// Goals =// "iGoals"if (input.contains(tag)) {
                    StringstrValue= input.substring(
                            input.indexOf(tag) + tag.length(),
                            input.indexOf(";", input.indexOf(tag)));
                    if (strValue.length() != 0) {
                        fields[i].setInt(output, Integer.valueOf(strValue));
                    }
                }
            }

            // detect float or Floatif (type.equals(Float.TYPE) || type.equals(Float.class)) {
                Stringtag="f" + fields[i].getName() + "=";
                if (input.contains(tag)) {
                    StringstrValue= input.substring(
                            input.indexOf(tag) + tag.length(),
                            input.indexOf(";", input.indexOf(tag)));
                    if (strValue.length() != 0) {
                        fields[i].setFloat(output, Float.valueOf(strValue));
                    }
                }
            }
        }

    }
}

Also Tutorial with Source code

see this link

Solution 2:

Try this code snippet

publicstatic Category[] RetrieveFromSoap(SoapObject soap)
    {
        Category[] categories = newCategory[soap.getPropertyCount()];
        for (inti=0; i < categories.length; i++) {
            SoapObjectpii= (SoapObject)soap.getProperty(i);
            Categorycategory=newCategory();
            category.CategoryId = Integer.parseInt(pii.getProperty(0).toString());
            category.Name = pii.getProperty(1).toString();
            category.Description = pii.getProperty(2).toString();
            categories[i] = category;
        }
        return categories;
    }

Post a Comment for "How To Do The Parsing Of Soap Response"