You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Ksoap2无法序列化int数组,调用.NET Web服务报错求助

向.NET Web服务发送含int数组对象时的序列化错误解决

尝试向.NET Web服务发送包含int数组的对象时,抛出错误:java.lang.RuntimeException: Cannot serialize [10,9,1]

预期请求XML格式

<Token>string</Token>
<Type>string</Type>
<DownloadSelectionInfo>
  <filesListCount>int</filesListCount>
  <filesList>
    <int>int</int>
    <int>int</int>
  </filesList>
  <Filename>string</Filename>
</DownloadSelectionInfo>

自定义KvmSerializable实现类

public class DownloadSelectionInfo implements KvmSerializable {
    ....
    private ArrayList<Integer> filesList;
    ...
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
          ...
            case 1:
                info.type = PropertyInfo.VECTOR_CLASS;;
                info.name = "filesList";
                break;
         ...
    }

    public void setProperty(int index, Object value) {
              ....
            case 1:
                filesList.add(Integer.parseInt(value.toString()));
            .....
    }
}

Web服务调用代码

//add filesList
SoapObject soFilesList = new SoapObject(Util.getWebService(),"filesList");
for(Integer i : downloadSelectionInfo.getFilesList()){
    soFilesList.addProperty("int",i);
}
soDownloadSelectionInfo.addSoapObject(soFilesList);

PropertyInfo pi = new PropertyInfo();
pi.setName("DownloadSelectionInfo");
pi.setValue(downloadSelectionInfo);
pi.setType(DownloadSelectionInfo.class);
Request.addProperty(pi);

soapEnvelope.addMapping(Util.getWebService(), "DownloadSelectionInfo",DownloadSelectionInfo.class);

解决方法

1. 修正集合类型的序列化配置

当前getPropertyInfo中使用PropertyInfo.VECTOR_CLASS,但实际用的是ArrayList,类型不匹配导致序列化失败。可以:

  • 改用Vector<Integer>作为集合类型,保持info.type = PropertyInfo.VECTOR_CLASS不变:
    private Vector<Integer> filesList;
    
  • 若继续用ArrayList,需指定正确的类型:
    case 1:
        info.type = ArrayList.class;
        info.name = "filesList";
        break;
    

2. 调整setProperty方法逻辑

如果使用ArrayList,直接接收集合对象而非逐个解析字符串:

case 1:
    filesList = (ArrayList<Integer>) value;
    break;

3. 简化Soap请求构建

不需要手动构建soFilesList,直接将集合作为属性添加,让KVM自动处理序列化:

// 替换原来的filesList构建代码
soDownloadSelectionInfo.addProperty("filesList", downloadSelectionInfo.getFilesList());

4. 匹配.NET服务端类型

确认.NET服务端的filesList参数类型为int[]或List<int>:

  • 若服务端期望数组,客户端将filesList改为Integer[],并在getPropertyInfo中设置info.type = Integer[].class,setProperty中直接赋值:
    case 1:
        filesList = (Integer[]) value;
        break;
    

内容的提问来源于stack exchange,提问作者D.Djaber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 14:15:32