2015-01-16 115 views
0

我想打电话从Android的一个SOAP .NET Web服务上,需要包含一个这样的元素(从.NET Web服务客户端生成)的请求:KSOAP - 请求与属性属性和名称空间的价值

<InputVariable> 
    <Id>MyVariable</Id> 
    <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">Hello</Value> 
</InputVariable> 

这里有一些奇怪的事情,kSOAP似乎并不直接支持。其一,我在kSOAP中找不到一种方法来生成一个也有属性的属性。我发现this answer能够让我进一步,但仍然不确切。以下是我得到与解决方案:

<InputVariable> 
    <Id>MyVariable</Id> 
    <Value i:type="http://www.w3.org/2001/XMLSchema:string">Hello</Value> 
</InputVariable> 

SoapObject inputVariable = new SoapObject("", "InputVariable"); 
inputVariable.addProperty("Id", "MyVariable"); 

AttributeInfo att = new AttributeInfo(); 
att.setName("type"); 
att.setNamespace("http://www.w3.org/2001/XMLSchema-instance"); 
// I need some way to set the value of this to a namespaced string 
att.setValue("http://www.w3.org/2001/XMLSchema:string"); 

ValueSoapObject valueProperty = new ValueSoapObject("", "Value"); 
valueProperty.setText("Hello"); 
valueProperty.addAttribute(att); 
inputVariable.addSoapObject(valueProperty); 

在运行时,服务器因错误而失败,它不能反序列化: Value' contains data from a type that maps to the name '://www.w3.org/2001/XMLSchema:string'. The deserializer has no knowledge of any type that maps to this name.

我怎么能产生这种类型SOAP属性使用kSOAP for Android?

回答

1

我不确定如果这是您的问题万能的,但SoapSerializationEnvelope :: implicitTypes设置为false,强制将值添加到值。所以fe。您的要求的简单vesion建这样的:

SoapObject request = new SoapObject("", "InputVariable"); 
request.addProperty("Id", "MyVariable"); 
request.addProperty("Value", "Hello"); 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet=true; 
envelope.implicitTypes = false; 
envelope.setOutputSoapObject(request); 

产生这样的要求:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"> 
<v:Header /> 
    <v:Body> 
     <InputVariable xmlns="" id="o0" c:root="1"> 
      <Id i:type="d:string">MyVariable</Id> 
      <Value i:type="d:string">Hello</Value> 
     </InputVariable> 
    </v:Body> 
</v:Envelope> 

也许是你的WS会喜欢这个;) 问候,马尔辛 -

+0

这工作!至少,解析问题。现在我遇到了这个http://stackoverflow.com/questions/10014164/android-ksoap2-nullable-type,但那个答案也有效。谢谢! – Travis

相关问题