kSoap2序列化错误

发布于 2024-11-14 21:40:58 字数 2944 浏览 3 评论 0原文

我正在使用 kSoap2 从 Android 设备连接到 WCF 服务,但出现序列化错误:

Cannot serialize: CompositeType : StringValue = Test, BoolValue = true

这是 Web 服务调用的代码:

public void call() {
    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        CompositeType comp = new CompositeType();
        comp.setProperty(CompositeType.STRING_VALUE, "Test");
        comp.setProperty(CompositeType.BOOLEAN_VALUE, true);
        PropertyInfo pi = new PropertyInfo();
        pi.setName("CompositeType");
        pi.setValue(comp);
        pi.setType(comp.getClass());
        request.addProperty("composite", pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        envelope.addMapping(NAMESPACE, "CompositeType", (new CompositeType()).getClass());

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result = (SoapObject) envelope.getResponse();

        comp.setProperty(CompositeType.BOOLEAN_VALUE, Boolean.parseBoolean(result.getProperty(0).toString()));
        comp.setProperty(CompositeType.STRING_VALUE, result.getProperty(1).toString());

        sb.append(comp.toString());
    } catch (Exception e) {
        e.printStackTrace();
        sb.append("Error:\n" + e.getMessage() + "\n");
    }

}

这是我的代码CompositeType 类:

public class CompositeType implements KvmSerializable{

public static final int STRING_VALUE = 1;
public static final int BOOLEAN_VALUE = 0;

public String StringValue;
public Boolean BoolValue;

public CompositeType() {
}

@Override
public String toString() {
    return "StringValue = " + StringValue + ", BoolValue = " + BoolValue.toString();
}

@Override
public Object getProperty(int arg0) {
    switch(arg0)
    {
    case STRING_VALUE:
        return StringValue;
    case BOOLEAN_VALUE:
        return BoolValue;
    }

    return null;
}

@Override
public int getPropertyCount() {
    return 2;
}

@Override
public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
    switch(index)
    {
    case STRING_VALUE:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "StringValue";
        break;
    case BOOLEAN_VALUE:
        info.type = PropertyInfo.BOOLEAN_CLASS;
        info.name = "BoolValue";
        break;
    default:break;
    }
}

@Override
public void setProperty(int index, Object value) {
    switch(index)
    {
    case STRING_VALUE:
        StringValue = value.toString();
        break;
    case BOOLEAN_VALUE:
        BoolValue = Boolean.parseBoolean(value.toString());
        break;
    default:
        break;
    }
}


}

我认为实现 KvmSerialized 的对象可以被序列化......有谁知道如何让它工作?

I'm using kSoap2 to connect to a WCF service from an android device but I get an serialisation error:

Cannot serialize: CompositeType : StringValue = Test, BoolValue = true

Here is the code for the webservice call:

public void call() {
    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        CompositeType comp = new CompositeType();
        comp.setProperty(CompositeType.STRING_VALUE, "Test");
        comp.setProperty(CompositeType.BOOLEAN_VALUE, true);
        PropertyInfo pi = new PropertyInfo();
        pi.setName("CompositeType");
        pi.setValue(comp);
        pi.setType(comp.getClass());
        request.addProperty("composite", pi);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        envelope.addMapping(NAMESPACE, "CompositeType", (new CompositeType()).getClass());

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result = (SoapObject) envelope.getResponse();

        comp.setProperty(CompositeType.BOOLEAN_VALUE, Boolean.parseBoolean(result.getProperty(0).toString()));
        comp.setProperty(CompositeType.STRING_VALUE, result.getProperty(1).toString());

        sb.append(comp.toString());
    } catch (Exception e) {
        e.printStackTrace();
        sb.append("Error:\n" + e.getMessage() + "\n");
    }

}

And this is my code for the CompositeType class:

public class CompositeType implements KvmSerializable{

public static final int STRING_VALUE = 1;
public static final int BOOLEAN_VALUE = 0;

public String StringValue;
public Boolean BoolValue;

public CompositeType() {
}

@Override
public String toString() {
    return "StringValue = " + StringValue + ", BoolValue = " + BoolValue.toString();
}

@Override
public Object getProperty(int arg0) {
    switch(arg0)
    {
    case STRING_VALUE:
        return StringValue;
    case BOOLEAN_VALUE:
        return BoolValue;
    }

    return null;
}

@Override
public int getPropertyCount() {
    return 2;
}

@Override
public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
    switch(index)
    {
    case STRING_VALUE:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "StringValue";
        break;
    case BOOLEAN_VALUE:
        info.type = PropertyInfo.BOOLEAN_CLASS;
        info.name = "BoolValue";
        break;
    default:break;
    }
}

@Override
public void setProperty(int index, Object value) {
    switch(index)
    {
    case STRING_VALUE:
        StringValue = value.toString();
        break;
    case BOOLEAN_VALUE:
        BoolValue = Boolean.parseBoolean(value.toString());
        break;
    default:
        break;
    }
}


}

I thought objects that implement KvmSerializable can be serialized ... Does anyone know how to get it working?

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

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

发布评论

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

评论(1

眼泪也成诗 2024-11-21 21:40:58

这个回复可能太少太晚了,但我注意到布尔参数有一个问题。

我刚刚将布尔值更改为整数,我的服务运行良好!您还需要将 Web 服务参数/对象设置为使用 int,然后根据需要转换为 bool!

This reply might be too little and too late, but I have noticed a problem with boolean params.

I just changed the booleans to ints and my services worked fine! you will also need to set the web service params / objects to use int's and then convert to bools if you like!

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