使用 ksoap2-Android 发送复杂对象、附件
我在 Android 项目上使用 ksoap2-Android 来上传文件。它不起作用。
首先,我的 wsdl 看起来像这样:
<xsd:element name="Op1RequestType">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="date" type="xsd:dateTime"/>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="imgFile"
type="tns:Attachment"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
“tns:Attachment”的定义如下:
<xsd:complexType name="Attachment">
<xsd:sequence>
<xsd:element name="file" type="xsd:base64Binary"/>
<xsd:element name="filename" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
我正在创建一个 SoapSerializationEnvelope
并添加名称为 date
的属性和作为当前日期的字符串表示形式的值。即使我不添加文件(请注意 minOccurs="0"
),此操作也会成功。但是,当我尝试添加文件时,它严重失败:
首先,我通过创建具有 file 属性的
和 SoapObject
来代表 Attachment
类型filename
,分别为 byte[].class
和 String.class
类型。
然后,我将这些对象添加到通用 Vector(以表示 imgFile
项的多重性)并将该 Vector 作为属性附加到信封。这成功创建了一条 SOAP 消息,并且来自服务器的响应引发了异常(因为这是一条错误消息,而不是正确的响应,因为不知何故我的输入不好......):
WARN/System.err(438): SoapFault - faultcode: 'soapenv:Server'
faultstring:'org.apache.axis2.databinding.ADBException: Unexpected subelement imgFile'
faultactor: 'null' detail: org.kxml2.kdom.Node@4676b8a0
好的,那么我做错了什么?有没有办法查看我发送的请求 SOAP 信封?
I'm using ksoap2-Android on an Android project to upload a file. It's not working.
First of all, my wsdl looks like this:
<xsd:element name="Op1RequestType">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="date" type="xsd:dateTime"/>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="imgFile"
type="tns:Attachment"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
And "tns:Attachment" is defined like this:
<xsd:complexType name="Attachment">
<xsd:sequence>
<xsd:element name="file" type="xsd:base64Binary"/>
<xsd:element name="filename" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
I'm creating a SoapSerializationEnvelope
and adding in the property with name date
and the value as the string representation of the current date. This works successfully, even if I don't add a file (note the minOccurs="0"
). However, when I try to add a file, it fails horribly:
First, I make a representative of the Attachment
type by creating a SoapObject
which has the properties file
and filename
, of types byte[].class
and String.class
respectively.
Then I add these objects to a generic Vector (to represent the multiplicity of the imgFile
item) and attach the Vector as a property to the envelope. This creates a SOAP message successfully, and the response from the server raises an exception (because it's an error message, instead of a proper response, because somehow my input isn't good...):
WARN/System.err(438): SoapFault - faultcode: 'soapenv:Server'
faultstring:'org.apache.axis2.databinding.ADBException: Unexpected subelement imgFile'
faultactor: 'null' detail: org.kxml2.kdom.Node@4676b8a0
Okay, so what am I doing wrong? Is there a way to see the request SOAP envelope that I am sending?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以通过在 SoapSerializationEnvelope 上调用
getRequest()
来查看请求 SOAP 信封。这让我看到矢量对象实际上将每个(文件,文件名)对放入
标记中,这破坏了格式。我现在按照 wsdl 的要求连续插入多个项目。I can see the request SOAP envelope by calling
getRequest()
on the SoapSerializationEnvelope. This allowed me to see that the vector object actually places each (file, filename) pair into an<item></item>
tag, which broke the format. I now am inserting multiple items in succession as the wsdl demands.