Issue in looping to parse

jenil

I have an xml with 'n' number of data which i am parsing,for test i hardcoded without looping has below,now the below line is just parsing and showing the data for index '1' ,i need to loop this and i am not sure how can i do this.How do i find the length of obj and loop,i cannot find any method in SoapObject.I used like below but the data is getting overridden after parsing

  GetReminder getReminder=new GetReminder();
    SoapObject obj=(SoapObject)envelope.getResponse();
   for(int i=0;i<obj.getPropertyCount();i++) {
   KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(), getReminder);}

and the parser is

public static void parseBusinessObject(String input, Object output) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, InstantiationException{
       System.out.println("input----> "  +input);
        Class theClass = output.getClass();
        Field[] fields = theClass.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            Type type=fields[i].getType();
            System.out.println("type--" +type);
            fields[i].setAccessible(true);

            //detect String
            if (fields[i].getType().equals(String.class)) {
                String tag =  fields[i].getName() + "=";   //"s" is for String in the above soap response example + field name for example Name = "sName"
                System.out.println("fff------------"+tag);
                if(input.contains(tag)){
                    String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
                    System.out.println("strvalue------------"+strValue);

                    if(strValue.length()!=0){
                        fields[i].set(output, strValue);
                    }
                }
            }

            //detect int or Integer
            if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
                String tag = 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)){
                    String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
                    System.out.println("strvalue------------"+strValue);

                    if(strValue.length()!=0){
                        fields[i].setInt(output, Integer.valueOf(strValue));
                    }
                }
            }

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

You just have to do a for loop on the property count available in the SaopObject.

SoapObject obj=(SoapObject)envelope.getResponse();
int nbProperties = obj.getPropertyCount();

for (int i = 0; i < nbProperties; i++) {
   KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(), getReminder);
   // Do something with getReminder object
}

Note: if the ouput Object is added to a list for instance, then you should allocate a new ouput object in order for each call to parseBusinessObject() to set the field in a fresh object. E.g.

// Create the list that shall contains the read properties
List<GetReminder> reminders = new ArrayList<>();

SoapObject obj=(SoapObject)envelope.getResponse(); 
Log.d("Result --ddd- ", obj.toString() ); 
System.out.println("obj---->" + obj.getPropertyCount()); 

for(int i=0; i < obj.getPropertyCount(); i++) {
   System.out.println("here i is.........." +i);

   // Create a new instance
   GetReminder rem = new GetReminder();

   // Read and set the fields values of rem
   KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(), rem); 

   // Don't forget to store the new reminder in the list
   reminders.add(rem);
}   

// Do what you want with the list of reminders

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related