如何使用ksoap2传递字符串数组web服务? [英] How to pass String array to webservice using ksoap2?
问题描述
我有一个Web客户端在Android中使用 ksoap2
,但我不能把这个字符串数组作为参数传递给Web服务。
下面是我的code
的String []项目= {你好,世界};
request.addproperty(海峡,项目);
首先使用的soapUI看到正确的请求结构(如项目名称,项目名称空间,...)。 我们假设你要这样写的XML请求:(这里N0和N1的命名空间)
< N0:strarray的xmlns:N0 =HTTP:// N0 ......的xmlns:N1 =HTTP:// N1 ......>
< N1:字符串>你好< / N1:字符串>
< N1:字符串>世界< / N1:字符串>
< / N0:strarray>
从矢量扩展类:
进口java.util.Hashtable中;
进口java.util.Vector中;
进口org.ksoap2.serialization.KvmSerializable;
进口org.ksoap2.serialization.PropertyInfo;
公共类StringArraySerializer扩展矢量<串GT;实现KvmSerializable {
// N1店项目的命名空间:
串N1 =的http:// N1 ......;
@覆盖
公共对象的getProperty(INT为arg0){
返回this.get(为arg0);
}
@覆盖
公众诠释getPropertyCount(){
返回this.size();
}
@覆盖
公共无效为getPropertyInfo(INT为arg0,哈希表ARG1,ARG2的PropertyInfo){
arg2.setName =字符串;
arg2.type = PropertyInfo.STRING_CLASS;
arg2.setNamespace = N1;
}
@覆盖
公共无效的SetProperty(INT为arg0,对象ARG1){
this.add(arg1.toString());
}
}
要建设的要求,你必须这样做:
1,使一个新的矢量对象从这个类:
StringArraySerializer字符串数组=新StringArraySerializer();
2,那么你可以添加元素:
stringArray.add(你好);
stringArray.add(世界);
3,然后创建一个的PropertyInfo它:
// N0店阵列的命名空间:
字符串N0 =HTTP:// N0 ......;
stringArrayProperty =新的PropertyInfo();
stringArrayProperty.setName(strarray);
stringArrayProperty.setValue(字符串数组);
stringArrayProperty.setType(stringArray.getClass());
stringArrayProperty.setNamespace(N0);
4,那么你的所有属性添加到请求:
请求=新SoapObject(命名空间METHOD_NAME);
Request.addProperty(stringArrayProperty);
参考:
ksoap2-的Android,CodingTipsAndTricks
I am having a Web Client in Android using ksoap2
but I can't pass the string array as a parameter to the webservice.
Here's my code
String[] items={"hello","world"};
request.addproperty("str",items);
First use "soapUI" to see correct request structure(like item names,item namespaces , ...). We assume that you want to write like this XML in request:(here n0 and n1 are namespaces)
<n0:strarray xmlns:n0="http://n0 ..." xmlns:n1="http://n1 ...">
<n1:string>hello</n1:string>
<n1:string>world</n1:string>
</n0:strarray>
extend a class from vector:
import java.util.Hashtable;
import java.util.Vector;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class StringArraySerializer extends Vector<String> implements KvmSerializable {
//n1 stores item namespaces:
String n1 = "http://n1 ...";
@Override
public Object getProperty(int arg0) {
return this.get(arg0);
}
@Override
public int getPropertyCount() {
return this.size();
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
arg2.setName = "string";
arg2.type = PropertyInfo.STRING_CLASS;
arg2.setNamespace = n1;
}
@Override
public void setProperty(int arg0, Object arg1) {
this.add(arg1.toString());
}
}
To build the request you have to do this:
1-make a new Vector-Object from this class:
StringArraySerializer stringArray = new StringArraySerializer();
2-then you can add elements:
stringArray.add("hello");
stringArray.add("world");
3-then you create a PropertyInfo with it:
//n0 stores array namespace:
String n0 = "http://n0 ...";
stringArrayProperty = new PropertyInfo();
stringArrayProperty.setName("strarray");
stringArrayProperty.setValue(stringArray);
stringArrayProperty.setType(stringArray.getClass());
stringArrayProperty.setNamespace(n0);
4-then you add all the properties to the request:
Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty(stringArrayProperty);
Reference:
ksoap2-android,CodingTipsAndTricks
这篇关于如何使用ksoap2传递字符串数组web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!