我在Android中使用ksoap2有一个Web客户端,但是我无法将字符串数组作为参数传递给webservice。我在下面附上了我的代码和服务。
服务:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mys="http://182.73.50.69:6510/Myservice" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays ">
<soapenv:Header/>
<soapenv:Body>
<mys:addworkload>
<!--Optional:-->
<mys:member>Antony</mys:member>
<!--Optional:-->
<mys:prj>
<!--Zero or more repetitions:-->
<arr:string>Prj1</arr:string>
<arr:string>Prj2</arr:string>
<arr:string>Prj3</arr:string>
</mys:prj>
<!--Optional:-->
<mys:val>
<!--Zero or more repetitions:-->
<arr:int>2</arr:int>
<arr:int>2</arr:int>
<arr:int>2</arr:int>
</mys:val>
<!--Optional:-->
<mys:date>2016-05-29</mys:date>
</mys:addworkload>
</soapenv:Body>
</soapenv:Envelope>
Android代码:
SoapObject Search= new SoapObject(Constants.NAMESPACE, "addworkload");
List<String> companies = new ArrayList<>();
companies.add("Prj1");
companies.add("Prj2");
companies.add("Prj3");
List<String> ones = new ArrayList<>();
ones.add("2");
ones.add("2");
ones.add("2");
SoapObject soapCompanies = new SoapObject(Constants.NAMESPACE, "prj");
for (String i : companies){
soapCompanies.addProperty("string", i);
}
request.addSoapObject(soapCompanies);
SoapObject val = new SoapObject(Constants.NAMESPACE, "val");
for (String i : ones){
val.addProperty("int", i);
}
request.addSoapObject(val);
请告诉我我在哪里错过了这里的链接..
答案 0 :(得分:1)
您将需要两个自定义可序列化类,这些类扩展了您要保留的类型的集合。它还应该实现KvmSerializable
。
String
数组的类,
public class CustomKvmSerializable extends Vector<String> implements KvmSerializable {
private String tag;
private Class klass;
public CustomKvmSerializable(String tag, Class klass) {
this.tag = tag;
this.klass = klass;
}
@Override
public Object getProperty(int i) {
return this.get(i);
}
@Override
public int getPropertyCount() {
return 1;
}
@Override
public void setProperty(int i, Object o) {
this.add(o.toString());
}
@Override
public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
propertyInfo.name = tag;
propertyInfo.type = klass;
}
@Override
public String getInnerText() {
return null;
}
@Override
public void setInnerText(String s) {
}
}
对于整数数组,创建一个扩展Vector<Integer>
的类似类。
要在您的请求中使用此功能,
ArrayList<PropertyInfo> props = new ArrayList<>();
// For string array
CustomKvmSerializable stringVector = new CustomKvmSerializable("string", String.class);
for(String str : yourStringArray){
stringVector.add(str);
}
PropertyInfo instrumentInfo = new PropertyInfo();
instrumentInfo.setName("prj");
instrumentInfo.setValue(stringVector);
instrumentInfo.setType(stringVector.getClass());
props.add(instrumentInfo);
// For integer array
CustomKvmSerializable integerVector = new CustomKvmSerializable("int", String.class);
for(Integer integer : yourIntegerArray){
integerVector.add(integer);
}
PropertyInfo instrumentInfo = new PropertyInfo();
instrumentInfo.setName("val");
instrumentInfo.setValue(integerVector);
instrumentInfo.setType(integerVector.getClass());
props.add(instrumentInfo);
创建请求时,
SoapObject search = new SoapObject(Constants.NAMESPACE, "addworkload");
// Add the properties to your request
for (PropertyInfo property : props) {
request.addProperty(property);
}