使用 KSOAP2 将 Array<String> 发送到 Web 服务

Send Array<String> to a web service using KSOAP2

我的 Android 项目使用 KSOAP2 时遇到问题。我想向网络服务发送新的标签检测。我的 Web 服务中有一个名为 AddNewDetection 的方法。我放了一张功能描述的照片。我有这个错误

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

我必须向 Web 服务传递一个字符串数组。我做不到。我正在使用 Kotlin 1.3.50 和 KSOAP2 3.6.4

我尝试了不同的方法,例如使用 ArrayList,...我没有找到解决问题的方法。

提前感谢您的帮助!

    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

    }`

我找到了解决办法。我需要创建一个 class 来扩展 Vector 并实现 KvmSerializable。然后将命名空间设置为“http://schemas.microsoft.com/2003/10/Serialization/Arrays”。

这是我的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);
}

}

这是我将字符串数组添加到 属性 信息的方法:

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