Android 如何使用ksoap2将字符串数组传递给webservice?
我有一个Android Web客户端,使用Android 如何使用ksoap2将字符串数组传递给webservice?,android,web-services,android-ksoap2,Android,Web Services,Android Ksoap2,我有一个Android Web客户端,使用ksoap2,但我无法将字符串数组作为参数传递给Web服务 这是我的密码 String[] items={"hello","world"}; request.addproperty("str",items); 首先使用“soapUI”查看正确的请求结构(如项目名称、项目名称空间等)。 我们假设您希望在请求中像这样编写XML:(这里n0和n1是名称空间) 2-然后您可以添加元素: stringArray.add("hello"); stringArray
ksoap2
,但我无法将字符串数组作为参数传递给Web服务
这是我的密码
String[] items={"hello","world"};
request.addproperty("str",items);
首先使用“soapUI”查看正确的请求结构(如项目名称、项目名称空间等)。
我们假设您希望在请求中像这样编写XML:(这里n0和n1是名称空间)
2-然后您可以添加元素:
stringArray.add("hello");
stringArray.add("world");
3-然后使用它创建PropertyInfo:
//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-然后将所有属性添加到请求中:
Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty(stringArrayProperty);
参考:
就像你应该一个接一个地添加它一样
public class ExtendedSoapObject extends SoapObject
{
public ExtendedSoapObject(String namespace, String name)
{
super(namespace, name);
}
public ExtendedSoapObject(SoapObject o)
{
super(o.getNamespace(), o.getName());
for (int i = 0; i < o.getAttributeCount(); i++)
{
AttributeInfo ai = new AttributeInfo();
o.getAttributeInfo(i, ai);
ai.setValue(o.getAttribute(i));
addAttribute(ai);
}
for (int i = 0; i < o.getPropertyCount(); i++)
{
PropertyInfo pi = new PropertyInfo();
o.getPropertyInfo(i, pi);
pi.setValue(o.getProperty(i));
addProperty(pi);
}
}
@Override
public SoapObject addProperty(String name, Object value)
{
if (value instanceof Object[])
{
Object[] subValues = (Object[]) value;
for (int i = 0; i < subValues.length; i++)
{
super.addProperty(name, subValues[i]);
}
}
else
{
super.addProperty(name, value);
}
return this;
}
@Override
public Object getProperty(String name)
{
List<Object> result = new ArrayList<Object>();
for (int i = 0; i < properties.size(); i++)
{
PropertyInfo prop = (PropertyInfo) properties.elementAt(i);
if (prop.getName() != null && prop.getName().equals(name))
{
result.add(unwrap(prop));
}
}
if (result.size() == 1)
{
return result.get(0);
}
else if (result.size() > 1)
{
return result.toArray(new Object[0]);
}
else
{
return null;
}
}
public Object[] getArrayProperty(String name)
{
Object o = getProperty(name);
Object values[] = null;
if (o != null)
{
if (o instanceof Object[])
{
values = (Object[]) o;
}
else
{
values = new Object[1];
values[0] = o;
}
}
return values;
}
Object unwrap(Object o)
{
if (o instanceof PropertyInfo)
{
return unwrap(((PropertyInfo) o).getValue());
}
else if (o instanceof SoapPrimitive || o instanceof SoapObject)
{
return o;
}
return null;
}
}
公共类ExtendedSoapObject扩展了SoapObject
{
公共扩展SOAPObject(字符串命名空间、字符串名称)
{
super(名称空间、名称);
}
公共扩展SoapObject(SoapObject o)
{
super(o.getNamespace(),o.getName());
对于(int i=0;i1)
{
返回result.toArray(新对象[0]);
}
其他的
{
返回null;
}
}
公共对象[]getArrayProperty(字符串名称)
{
对象o=getProperty(名称);
对象值[]=null;
如果(o!=null)
{
if(o对象[]的实例)
{
值=(对象[])o;
}
其他的
{
值=新对象[1];
值[0]=o;
}
}
返回值;
}
对象展开(对象o)
{
if(o属性info的实例)
{
返回展开(((PropertyInfo)o).getValue());
}
else if(o SoapPrimitive的instanceof | | o SoapObject的instanceof)
{
返回o;
}
返回null;
}
}
您需要在答案中总结这些示例,否则可能会被删除。如果链接断开,答案也是:)我必须在n1和n0中放置什么名称空间???@VivekSingh最简单的方法是:使用SoapUI并查看正确的请求。你可以在那里找到名称空间。@hasanghaforian:我需要你的帮助。请参阅此链接cani如何将数组数据传递给SOAP服务?这种方式也适用于HashMap?
//n0 stores array namespace:
String n0 = "http://n0 ...";
stringArrayProperty = new PropertyInfo();
stringArrayProperty.setName("strarray");
stringArrayProperty.setValue(stringArray);
stringArrayProperty.setType(stringArray.getClass());
stringArrayProperty.setNamespace(n0);
Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty(stringArrayProperty);
public class ExtendedSoapObject extends SoapObject
{
public ExtendedSoapObject(String namespace, String name)
{
super(namespace, name);
}
public ExtendedSoapObject(SoapObject o)
{
super(o.getNamespace(), o.getName());
for (int i = 0; i < o.getAttributeCount(); i++)
{
AttributeInfo ai = new AttributeInfo();
o.getAttributeInfo(i, ai);
ai.setValue(o.getAttribute(i));
addAttribute(ai);
}
for (int i = 0; i < o.getPropertyCount(); i++)
{
PropertyInfo pi = new PropertyInfo();
o.getPropertyInfo(i, pi);
pi.setValue(o.getProperty(i));
addProperty(pi);
}
}
@Override
public SoapObject addProperty(String name, Object value)
{
if (value instanceof Object[])
{
Object[] subValues = (Object[]) value;
for (int i = 0; i < subValues.length; i++)
{
super.addProperty(name, subValues[i]);
}
}
else
{
super.addProperty(name, value);
}
return this;
}
@Override
public Object getProperty(String name)
{
List<Object> result = new ArrayList<Object>();
for (int i = 0; i < properties.size(); i++)
{
PropertyInfo prop = (PropertyInfo) properties.elementAt(i);
if (prop.getName() != null && prop.getName().equals(name))
{
result.add(unwrap(prop));
}
}
if (result.size() == 1)
{
return result.get(0);
}
else if (result.size() > 1)
{
return result.toArray(new Object[0]);
}
else
{
return null;
}
}
public Object[] getArrayProperty(String name)
{
Object o = getProperty(name);
Object values[] = null;
if (o != null)
{
if (o instanceof Object[])
{
values = (Object[]) o;
}
else
{
values = new Object[1];
values[0] = o;
}
}
return values;
}
Object unwrap(Object o)
{
if (o instanceof PropertyInfo)
{
return unwrap(((PropertyInfo) o).getValue());
}
else if (o instanceof SoapPrimitive || o instanceof SoapObject)
{
return o;
}
return null;
}
}