kSoap2 发送集合
我有 android 应用程序,它将一些数据发送到 .net web 服务(不是 wcf,简单的 web 服务) 在android应用程序上,我有类,它
public class OrderDTO extends BaseSoapObject
{
public int ID;
public Date OrderDate;
public UserDTO Owner;
public Vector<OrderItemDTO> Products;
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info)
{
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "ID";
break;
case 1:
info.type = MarshalDate.DATE_CLASS;
info.name = "OrderDate";
break;
case 2:
info.type = UserDTO.class;
info.name = "Owner";
break;
case 3:
info.type = PropertyInfo.VECTOR_CLASS;
info.name = "Products";
break;
default:
break;
}
}
}
在服务器端实现了KvmSerialized接口,我有类
public class OrderDTO
{
public virtual int ID { get; set; }
public virtual UserDTO Owner { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual OrderItemDTO[] Products { get; set; }
public OrderDTO()
{
}
}
和Web服务方法
[WebMethod]
public void SaveOrder(OrderDTO order)
{
try
{
// do somthing
}
catch (Exception e)
{
}
finally
{
}
}
我已经成功从android调用此方法,字段Owner和OrderDate已正确填充,但产品字段为空(不是 null,只是空)
我尝试使用 Array、ArrayList 和 Vector -
当我看到请求转储并看到唯一不同的是(集合项的标签名称:
<Products i:type="c:Array" c:arrayType="d:anyType[1]">
<item i:type="n0:OrderItemDTO">
<ID i:type="d:int">0</ID>
<AssignedCode i:type="d:string">0001</AssignedCode>
<Count i:type="d:int">5</Count>
</item>
</Products>
和在我的 wsdl:
<Products>
<OrderItemDTO>
<ID>int</ID>
<Count>int</Count>
<AssignedCode>string</AssignedCode>
</OrderItemDTO>
<OrderItemDTO>
<ID>int</ID>
<Count>int</Count>
<AssignedCode>string</AssignedCode>
</OrderItemDTO>
</Products>
和我的 android 代码 中)没有任何变化与服务交互:
OrderDTO dto = new OrderDTO(order);
SoapObject request = new SoapObject(Namespace, SaveOrderMethod);
PropertyInfo pi = new PropertyInfo();
pi.setName("order");
pi.setValue(dto);
pi.setType(dto.getClass());
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(Namespace, "OrderItemDTO", new OrderItemDTO().getClass());
envelope.addMapping(Namespace, "UserDTO", new UserDTO().getClass());
envelope.addMapping(Namespace, "OrderDTO", new OrderDTO().getClass());
Marshal dateMarshal = new MarshalDate();
dateMarshal.register(envelope);
HttpTransportSE androidHttpTransport = new HttpTransportSE(Url);
androidHttpTransport.debug = true;
try
{
androidHttpTransport.call(SaveOrderSoapAction, envelope);
}
catch (Exception e)
{
Log.e(Tag, e.getMessage());
}
i have android application, which send some data to .net web service (not wcf, simple web service)
on android application i have class, which implement KvmSerializable interface
public class OrderDTO extends BaseSoapObject
{
public int ID;
public Date OrderDate;
public UserDTO Owner;
public Vector<OrderItemDTO> Products;
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info)
{
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "ID";
break;
case 1:
info.type = MarshalDate.DATE_CLASS;
info.name = "OrderDate";
break;
case 2:
info.type = UserDTO.class;
info.name = "Owner";
break;
case 3:
info.type = PropertyInfo.VECTOR_CLASS;
info.name = "Products";
break;
default:
break;
}
}
}
on the server side i have class
public class OrderDTO
{
public virtual int ID { get; set; }
public virtual UserDTO Owner { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual OrderItemDTO[] Products { get; set; }
public OrderDTO()
{
}
}
and the web service method
[WebMethod]
public void SaveOrder(OrderDTO order)
{
try
{
// do somthing
}
catch (Exception e)
{
}
finally
{
}
}
i have succeed call this method from android, field Owner and OrderDate are correctly filled, but the Products field is empty (not null, just empty)
i try to use Array, ArrayList and Vector - no changes
when i see request dump and see that the only different there is (tag name of the item of collection:
<Products i:type="c:Array" c:arrayType="d:anyType[1]">
<item i:type="n0:OrderItemDTO">
<ID i:type="d:int">0</ID>
<AssignedCode i:type="d:string">0001</AssignedCode>
<Count i:type="d:int">5</Count>
</item>
</Products>
and in my wsdl:
<Products>
<OrderItemDTO>
<ID>int</ID>
<Count>int</Count>
<AssignedCode>string</AssignedCode>
</OrderItemDTO>
<OrderItemDTO>
<ID>int</ID>
<Count>int</Count>
<AssignedCode>string</AssignedCode>
</OrderItemDTO>
</Products>
and my android code for interaction with service:
OrderDTO dto = new OrderDTO(order);
SoapObject request = new SoapObject(Namespace, SaveOrderMethod);
PropertyInfo pi = new PropertyInfo();
pi.setName("order");
pi.setValue(dto);
pi.setType(dto.getClass());
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(Namespace, "OrderItemDTO", new OrderItemDTO().getClass());
envelope.addMapping(Namespace, "UserDTO", new UserDTO().getClass());
envelope.addMapping(Namespace, "OrderDTO", new OrderDTO().getClass());
Marshal dateMarshal = new MarshalDate();
dateMarshal.register(envelope);
HttpTransportSE androidHttpTransport = new HttpTransportSE(Url);
androidHttpTransport.debug = true;
try
{
androidHttpTransport.call(SaveOrderSoapAction, envelope);
}
catch (Exception e)
{
Log.e(Tag, e.getMessage());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了答案,我需要向 OrderDTO.getPropertyInfo 方法添加一些代码 PropertyInfo elementInfo = new PropertyInfo(); elementInfo.name = "OrderItemDTO"; elementInfo.type = new OrderItemDTO().getClass(); info.elementType = elementInfo;
i have found the answer i need to add some code to OrderDTO.getPropertyInfo method PropertyInfo elementInfo = new PropertyInfo(); elementInfo.name = "OrderItemDTO"; elementInfo.type = new OrderItemDTO().getClass(); info.elementType = elementInfo;