kSoap2 Android -- 强制转换类异常 (SoapObject)

发布于 2024-11-03 16:44:34 字数 2556 浏览 1 评论 0原文

我目前正在尝试使用常规 Ksoap 实现从我的 Web 服务中检索数组。但是,当尝试访问此数组时,我收到以下 logcat 错误:

 04-27 00:27:01.772: ERROR/AndroidRuntime(530): Caused by: java.lang.RuntimeException: Unable to 
 start activity ComponentInfo{android.waiter/android.waiter.TablesActivity}: 
 java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive

似乎由于某种原因,Soap 与我转换属性值的方式不一致。附件是问题代码(和相关方法)

public static TableDTO[] RetrieveFromSoap(SoapObject soap)
{
    TableDTO[] tables = new TableDTO[soap.getPropertyCount()];
    for (int i = 0; i < tables.length-1; i++) {
        SoapObject pii = (SoapObject) soap.getProperty(i);
        TableDTO table = new TableDTO();
        table.setTableID(Integer.parseInt(pii.getProperty(0).toString())); 
        table.setCapacity(Integer.parseInt(pii.getProperty(1).toString()));
        table.setStatusFlag(pii.getProperty(2).toString());
        table.setCurrentWaiter(pii.getProperty(3).toString());
        tables[i] = table;
    }
    return tables;
}

TableDTO 信息:

 @Override
public Object getProperty(int arg0) {
    switch(arg0)
    {
    case 0:
        return TableID;
    case 1:
        return Capacity;
    case 2:
        return StatusFlag;
    case 3:
        return currentWaiter;
    }
    return null;
}

@Override
public int getPropertyCount() {
    // TODO Auto-generated method stub
    return 3;
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
    switch(arg0){
    case 0:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "TableID";
        break;
    case 1:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "Capacity";
        break;
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "StatusFlag";
        break;
    case 3:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "currentWaiter";
        break;
default:break;
    }       

}

@Override
public void setProperty(int arg0, Object value) {
    switch(arg0)
    {
    case 0:
        TableID = Integer.parseInt(value.toString());
        break;
    case 1:
        Capacity = Integer.parseInt(value.toString());
        break;
    case 2:
        StatusFlag = value.toString();
        break;
    case 3:
        currentWaiter = value.toString();
        break;
    default :
        break;

    }

}

问题出在该行,

 SoapObject pii = (SoapObject) soap.getProperty(i);

它不喜欢那里的转换。我不知道如何解决这个问题。

感谢您的时间和帮助!

I'm currently trying to retrieve an array from my webservice by using regular Ksoap implementation. However, when trying to access this array I am getting the following logcat error:

 04-27 00:27:01.772: ERROR/AndroidRuntime(530): Caused by: java.lang.RuntimeException: Unable to 
 start activity ComponentInfo{android.waiter/android.waiter.TablesActivity}: 
 java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive

It appears that for some reason Soap is not getting along with the way I am casting my property values. Attached is the problem code (and related methods)

public static TableDTO[] RetrieveFromSoap(SoapObject soap)
{
    TableDTO[] tables = new TableDTO[soap.getPropertyCount()];
    for (int i = 0; i < tables.length-1; i++) {
        SoapObject pii = (SoapObject) soap.getProperty(i);
        TableDTO table = new TableDTO();
        table.setTableID(Integer.parseInt(pii.getProperty(0).toString())); 
        table.setCapacity(Integer.parseInt(pii.getProperty(1).toString()));
        table.setStatusFlag(pii.getProperty(2).toString());
        table.setCurrentWaiter(pii.getProperty(3).toString());
        tables[i] = table;
    }
    return tables;
}

TableDTO Information:

 @Override
public Object getProperty(int arg0) {
    switch(arg0)
    {
    case 0:
        return TableID;
    case 1:
        return Capacity;
    case 2:
        return StatusFlag;
    case 3:
        return currentWaiter;
    }
    return null;
}

@Override
public int getPropertyCount() {
    // TODO Auto-generated method stub
    return 3;
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
    switch(arg0){
    case 0:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "TableID";
        break;
    case 1:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "Capacity";
        break;
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "StatusFlag";
        break;
    case 3:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "currentWaiter";
        break;
default:break;
    }       

}

@Override
public void setProperty(int arg0, Object value) {
    switch(arg0)
    {
    case 0:
        TableID = Integer.parseInt(value.toString());
        break;
    case 1:
        Capacity = Integer.parseInt(value.toString());
        break;
    case 2:
        StatusFlag = value.toString();
        break;
    case 3:
        currentWaiter = value.toString();
        break;
    default :
        break;

    }

}

The problem is with the line

 SoapObject pii = (SoapObject) soap.getProperty(i);

It does not like the casting there. I'm at a loss on how to fix this.

Thanks for your time and help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-11-10 16:44:34

而不是使用

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

Use 。

SoapObject response = (SoapObject)envelope.bodyIn;

对我有用的

Instead of using

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

Use

SoapObject response = (SoapObject)envelope.bodyIn;

That worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文