本文介绍了在Ksoap2标签内嵌套的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我想用上面的SOAP XML做肥皂的请求
Hi i want to make a request for soap using above soap xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
<soapenv:Header/>
<soapenv:Body>
<urn:AvailCheck>
<IUvail>
<Unit>PC</Unit>
<Qty>3000</Qty>
</IUvail>
</urn:AvailCheck>
</soapenv:Body>
</soapenv:Envelope>
所以我用KSOAP库中创建一个code。我创建了一个对象soapobject
So I created a code using ksoap library. i created an object for soapobject
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
和添加这样的属性。
request.addProperty("Unit", "PC");
request.addProperty("Qty", "3000");
但问题是,我不能添加&LT; IUvail&GT;
在requset ..所以我如何添加此
but problem is i cant add <IUvail>
in the requset.. So how can i add this?
推荐答案
您可以通过一个复杂的对象(IUvail)是这样的:
You can pass a complex object (IUvail) like this:
IUvail.java
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
public class IUvail implements KvmSerializable {
private String unit;
private int qty;
public IUvail() {}
public IUvail(String unit, int qty) {
this.unit = unit;
this.qty = qty;
}
public void setUnit(String unit) { this.unit = unit; }
public void setQty(int qty) { this.qty = qty;}
public String getUniy() { return unit;}
public int getQty() { return qty;}
public Object getProperty(int arg0) {
switch(arg0) {
case 0:
return unit;
case 1:
return qty;
}
return null;
}
public int getPropertyCount() {
return 2;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo propertyInfo) {
switch(index):
case 0:
propertyInfo.name = "unit";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 1:
propertyInfo.name = "qty";
propertyInfo.type = PropertyInfo.INTEGER_CLASS;
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
switch(index) {
case 0:
this.unit = value.toString();
break;
case 1:
this.qty = Integer.parseInt(value,toString());
break;
default:
break;
}
}
然后
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
IUvail iuvail = new IUvail();
iuvail.setUnit("PC");
iuvail.setQty(3000);
request.addProperty("IUvail", iuvail);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(TARGET_NAMESPACE, "IUvail", new IUvail.getClass());
AndroidHttpTransport transport = new AndroidHttpTransport(URL);
try {
transport.call(NAMESPACE + METHOD_NAME, envelope);
/* Get the response: it depends on your web service implementation */
} catch (Exception e) {
e.printStackTrace();
}
其中的名称空间,网址,METHOD_NAME和TARGET_NAMESPACE取决于您的WS。
Where NAMESPACE, URL, METHOD_NAME and TARGET_NAMESPACE depends on your ws.
这篇关于在Ksoap2标签内嵌套的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!