循环解析的问题

时间:2015-03-14 14:40:20

标签: java web-services

我有一个带有' n'我正在解析的数据的数量,对于没有循环的硬编码的测试,现在下面的行只是解析并显示索引的数据' 1' ,我需要循环这个,我不知道我怎么能这样做。我如何找到obj和循环的长度,我在SoapObject中找不到任何方法。我使用如下,但数据在解析后被覆盖

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

并且解析器是

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));
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

您只需要对for中可用的媒体资源进行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
}

注意:如果将ouput对象添加到列表中,那么您应该分配一个新的输出对象,以便每次调用parseBusinessObject()来设置一个新的字段宾语。 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