java - 无法解析获取无效数组范围 : 5 to 5 的响应

标签 java json parsing exception indexoutofboundsexception

我有一个解析器来解析下面的响应,问题是我只能解析第一个表数据集,无法解析第二个或后面的表数据集,不确定循环哪里出了问题。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; }; }; }; }

我的解析代码没有正确解析整个响应,代码是

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

    }

}

我是从

打电话的
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);
 }

解析有问题,请帮我改正。

最佳答案

首先,仅供引用,您得到的响应不是 XML。

您有 2 个解决方案可以正确解析响应:

1- 获取实际 XML 形式的 WS 响应

要执行此操作,请参阅 this thread 。然后使用适当的XML DOM parser处理响应并构建 Java 对象。

2-直接使用 SOAP api 执行

在寻找最好的方法时,我遇到了this以及与您的问题相关的提示。 看parsing array elements 。根据您的情况,您可以更改代码: 更改行:

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

作者:

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

并更改方法parseBusinessObject(SoapObject pojoSoap, Object obj)的签名。然后在方法中,对于每种类型,例如对于字符串:

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

作者:

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

关于java - 无法解析获取无效数组范围 : 5 to 5 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29058925/

相关文章:

javascript - Unicode 编码字符串

c - yacc/bison 操作的范围是什么?

java - 如何在 scriptlet 标签中获取 jsp 页面的某些组件的值?

java - 读取新终端窗口的控制台输出

python - 将 csv 转换为 json,用于 Python 或 PHP 中的 JavaScript 绘图库 Chartjs

json - 如何从文本中提取需要的信息?

ruby-on-rails - 如何将 yaml 文件解析为 ruby​​ 哈希和/或数组?

带有图标名称的 Java 2d 数组

java - 线程 "main"java.lang.NoClassDefFoundError : backtype/storm/spout/MultiScheme 中出现异常

php - JSON Ajax PHP MYSQL - 多条记录到 JSON