How to pass String array to webservice using ksoap2?

孤街浪徒 提交于 2019-12-20 01:37:30

问题


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);

回答1:


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




回答2:


it is like that you shold add it one by one.

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;
        }
    }


来源:https://stackoverflow.com/questions/7130862/how-to-pass-string-array-to-webservice-using-ksoap2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!