androidkotlinksoap2android-ksoap2

Send Array<String> to a web service using KSOAP2


I am facing an issue with my Android project using KSOAP2. I would like to send a new tag detection to the web services. I got a method called AddNewDetection in my web services. I put a pic of the function description. I have this error

 W/System.err:     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:784)
 W/System.err:     at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:764)
 W/System.err:     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:688)
 W/System.err:     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBodyWithAttributes(SoapSerializationEnvelope.java:664)
 W/System.err:     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:777)
W/System.err:     at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:634)
 W/System.err:     at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:205)
W/System.err:     at org.ksoap2.transport.Transport.createRequestData(Transport.java:161)
W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:149)
 W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
 W/System.err:     at com.example.patrickrogerapp.common.QueryUtils.sendData(QueryUtils.kt:118)`

I have to pass to the web service a string array. And I am not able to do it. I am using Kotlin 1.3.50 and KSOAP2 3.6.4

I have tried different ways like using ArrayList, ... I did not find a solution to my problem.

Thank you in advance for any help!

    fun AddNewDetection(detectionDateTime : DateTime, idAction : int, idBox: int?, idProduit : int?, idShop: int?, numberList: Array<String>): Void

fun sendData(action: Int, idBox:Int?, idProduct: Int, idShop: Int, tags: Array<String>): SoapSerializationEnvelope {
        SOAP_ACTION = "http://tempuri.org/IStelStockService/AddNewDetection"

        val envelope = SoapSerializationEnvelope(SoapEnvelope.VER11)
        val request = SoapObject(NAMESPACE, "AddNewDetection")
        envelope.implicitTypes = true

        val pi = PropertyInfo()
        pi.setName("dectectionDateTime")
        pi.type = MarshalDate::class.java
        pi.value = Date(System.currentTimeMillis())
        MarshalDate().register(envelope)
        request.addSoapObject(SoapObject().addProperty(pi))

        request.addProperty("idAction", action)
        request.addProperty("idBox", idBox)
        request.addProperty("idProduit", idProduct)
        request.addProperty("idShop", idShop)
        request.addProperty("numberList", tags)

        val item = PropertyInfo()
        item.setType(Array<String>::class.java)
        item.setName("numberList")
        item.value = tags
        request.addProperty(item)

        envelope.dotNet = true
        envelope.setOutputSoapObject(request)

        val httpTransport = HttpTransportSE(URL)
        try {
            // This is the actual part that will call the webservice by setting the desired
            // header SOAP Action header field.
            httpTransport.call(SOAP_ACTION, envelope)
            Log.d("PR_DEBUG", envelope.response.toString())
        } catch (e: IOException) {
            // Many kinds of exceptions can be caught here
            Log.e(LOG_TAG, e.toString())
            e.printStackTrace()
        } catch (e: XmlPullParserException) {
            e.printStackTrace()
        }
        return envelope

    }`

Solution

  • I found a solution. I needed to create a class which extends Vector and implements KvmSerializable. Then set the namespace at "http://schemas.microsoft.com/2003/10/Serialization/Arrays".

    Here is my class:

    public class StringArraySerializer extends Vector<String> implements KvmSerializable {
    //n1 stores item namespaces:
    String n1 = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
    
    @Override
    public Object getProperty(int arg0) {
        return this.get(arg0);
    }
    
    @Override
    public int getPropertyCount() {
        return this.size();
    }
    
    @Override
    public void setProperty(int arg0, Object arg1) {
        this.add(arg1.toString());
    }
    
    @Override
    public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
        propertyInfo.setName("string");
        propertyInfo.setType(PropertyInfo.STRING_CLASS);
        propertyInfo.setNamespace(n1);
    }
    

    }

    And here is the way I add the string array to the property info:

    val list = StringArraySerializer()
        list.add("aaa")
        list.add("bb")
        val propertyInfo = PropertyInfo()
        propertyInfo.setName("numberList")
        propertyInfo.value = list
        propertyInfo.type = StringArraySerializer::class.java