简体   繁体   English

无法解析响应,数组范围无效:5到5

[英]Not able to parse the response getting Invalid array range: 5 to 5

I have a parser to parse the below response,the issue is i am able to parse only first table dataset not able to parse second or later table dataset,not sure where looping is going wrong.the xml response is like 我有一个解析器来解析以下响应,问题是我只能解析第一个表数据集而无法解析第二个或更高版本的表数据集,不确定循环在哪里出错.xml响应就像

anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; }; }; }; }; }; }; }; diffgram=anyType{NewDataSet=anyType{Table=anyType{RemMessage=Exeed Discount Limit on Invoice dated on 05/03/2015 for C SHAH , from 3 - Lokhandwala Showroom; InvM_Id=77693; DocType=3; PrmR_TypeId=3; PrmR_Id=1820; }; **Table=anyType{RemMessage=Exeed Discount Limit on Invoice dated on 14/03/2015 for G P SHAH , from 3 - Khar Showroom; InvM_Id=77800; DocType=3; PrmR_TypeId=3; PrmR_Id=1865; };** Table=anyType{RemMessage=Exeed Discount Limit on Invoice dated on 14/03/2015 for DOONGARSHI SHAH , from 3 - Khar Showroom; InvM_Id=77801; DocType=3; PrmR_TypeId=3; PrmR_Id=1866; }; }; }; }

my parsing code is not parsing entire response properly,The code is 我的解析代码无法正确解析整个响应,该代码是

public class KSoap2ResultParser {

    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("RemMessage------------"+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));
                    }
                }
            }
        }

    }

}

and i am calling from 我从打来

String response=androidHttpTransport.responseDump;

SoapObject obj=(SoapObject)envelope.getResponse();

for(int i=0; i < obj.getPropertyCount(); i++) {
    GetReminder rem = new GetReminder();
    KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(),rem);
    reminders.add(rem);
 }

there is an issue in parsing please help me to correct it. 解析中存在问题,请帮助我进行更正。

First, FYI, the response you get is not XML. 首先,仅供参考,您得到的响应不是 XML。

You have 2 solutions to properly parse the response: 您有2种解决方案可正确解析响应:

1- Get the WS response as an actual XML 1-获取WS响应作为实际的XML

To do that see this thread . 为此,请参见此线程 And then use a proper XML DOM parser to process the response and build the Java objects. 然后使用适当的XML DOM解析器来处理响应并构建Java对象。

2- Do it directly with a SOAP api 2-直接使用SOAP API进行操作

Looking for the best way, I came across this with tips that are relevant to your problem. 寻找最好的方式,我碰上了这个处理有关您的问题的技巧。 Look at parsing array elements . 看一下解析数组元素 In your case, you can change your code: Change the line: 您可以更改代码:更改行:

 KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(),rem); 

by: 通过:

 KSoap2ResultParser.parseBusinessObject((SoapObject)obj.getProperty(i),rem);

And change the signature of method parseBusinessObject(SoapObject pojoSoap, Object obj) . 并更改方法parseBusinessObject(SoapObject pojoSoap, Object obj)的签名。 Then in the method, for each type, eg for String : 然后在方法中,对于每种类型,例如对于String

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

by: 通过:

if(strValue.length()!=0){
     String fieldName = fields[i].getName();
     String fieldValue = pojoSoap.getProperty(fieldName).toString();
     fields[i].set(output, fieldValue);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM