我正在使用kSoap2从Android设备连接到WCF服务,但是我收到序列化错误:
无法序列化:CompositeType:StringValue = Test,BoolValue = true
以下是webservice调用的代码:
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;
}
}
}
我认为实现KvmSerializable的对象可以被序列化......有谁知道如何让它工作?
答案 0 :(得分:0)
这个回复可能太少而且太晚,但我注意到布尔参数有问题。
我刚刚将布尔值改为整数,我的服务运行良好!您还需要将Web服务参数/对象设置为使用int,然后根据需要转换为bools!