我正在尝试使用android-Ksoap2库从SOAP API获得响应。但是结果出乎我的意料。 API正常工作。我使用POST请求和XML对POSTMAN进行了测试。
我用XML文件发出的请求是..(已屏蔽名称空间)
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GetRealTimeStopData xmlns="******">
<stopId>1295</stopId>
<forceRefresh>true</forceRefresh>
</GetRealTimeStopData>
</Body>
</Envelope>
我尝试将SoapObject解析为有人建议的SoapPrimitive。但是,没有运气。
class SoapAsyncTask: AsyncTask<String, Void, String>() {
var URL = "******" //masking
var NAMESPACE = "******" // masking
var METHOD_NAME = "GetRealTimeStopData"
override fun doInBackground(vararg params: String): String {
var result = ""
val soapObject = SoapObject(NAMESPACE, METHOD_NAME)
val propertyInfo = PropertyInfo()
propertyInfo.type = Int::class.java
propertyInfo.name = "stopId"
propertyInfo.value = params[0]
soapObject.addProperty(propertyInfo)
val propertyInfo1 = PropertyInfo()
propertyInfo1.type = Boolean::class.java
propertyInfo1.name = "forceRefresh"
propertyInfo1.value = true
soapObject.addProperty(propertyInfo1)
Log.wtf("** DEBUG ** number of property ", soapObject.propertyCount.toString())
Log.wtf("** DEBUG ** stopId ", soapObject.getProperty(0).toString())
Log.wtf("** DEBUG ** forceRefresh ", soapObject.getProperty(1).toString())
val envelope = SoapSerializationEnvelope(SoapEnvelope.VER11)
envelope.setOutputSoapObject(soapObject)
envelope.dotNet = false
val httpTransportSE = HttpTransportSE(URL)
try {
httpTransportSE.call(getSoapAction(METHOD_NAME), envelope)
Log.wtf("** DEBUG ** envelope.response ", envelope.response.toString())
var resultObject: SoapObject = envelope.response as SoapObject
var soapPrimitiveObject: SoapPrimitive = resultObject as SoapPrimitive
result = soapPrimitiveObject.toString()
} catch (e: Exception) {
e.printStackTrace()
}
return result
}
override fun onPostExecute(s: String) {
Log.wtf("** DEBUG **", s)
}
private fun getSoapAction(method: String): String {
return "\"" + NAMESPACE + method + "\""
}
}
和错误日志如下。
2019-05-03 13:58:57.352 13506-13541/com.jinsoft77.soaptest E/** DEBUG ** number of property: 2
2019-05-03 13:58:57.374 13506-13541/com.jinsoft77.soaptest E/** DEBUG ** stopId: 1295
2019-05-03 13:58:57.391 13506-13541/com.jinsoft77.soaptest E/** DEBUG ** forceRefresh: true
2019-05-03 13:58:57.653 13506-13541/com.jinsoft77.soaptest D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-05-03 13:58:57.853 13506-13541/com.jinsoft77.soaptest E/** DEBUG ** envelope.response: anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; }; }; }; }; }; }; }; diffgram=anyType{}; }
2019-05-03 13:58:57.856 13506-13541/com.jinsoft77.soaptest W/System.err: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to org.ksoap2.serialization.SoapPrimitive
2019-05-03 13:58:57.856 13506-13541/com.jinsoft77.soaptest W/System.err: at com.jinsoft77.soaptest.SoapAsyncTask.doInBackground(SoapAsyncTask.kt:48)
2019-05-03 13:58:57.865 13506-13541/com.jinsoft77.soaptest W/System.err: at com.jinsoft77.soaptest.SoapAsyncTask.doInBackground(SoapAsyncTask.kt:12)
2019-05-03 13:58:57.865 13506-13541/com.jinsoft77.soaptest W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
2019-05-03 13:58:57.865 13506-13541/com.jinsoft77.soaptest W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-05-03 13:58:57.865 13506-13541/com.jinsoft77.soaptest W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2019-05-03 13:58:57.865 13506-13541/com.jinsoft77.soaptest W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
2019-05-03 13:58:57.865 13506-13541/com.jinsoft77.soaptest W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
2019-05-03 13:58:57.865 13506-13541/com.jinsoft77.soaptest W/System.err: at java.lang.Thread.run(Thread.java:764)
答案 0 :(得分:0)
我实际上解决了这个问题。为了将来参考,我写下了解决方法。
这是我的问题的一部分。
envelope.dotNet = true
完整方法。
fun getRealTimeStopData(stopId : String?, forceRefresh: String?) : MutableList<Bus> {
var busList: MutableList<Bus> = mutableListOf()
val METHOD_NAME = "GetRealTimeStopData"
val SOAP_ACTION = Utils.SOAP_NAMESPACE + METHOD_NAME
val soapObject = SoapObject(Utils.SOAP_NAMESPACE, METHOD_NAME)
soapObject.addProperty("stopId", stopId)
soapObject.addProperty("forceRefresh", forceRefresh)
val envelope = SoapSerializationEnvelope(SoapEnvelope.VER11)
envelope.setOutputSoapObject(soapObject)
envelope.dotNet = true // This should be TRUE *important
val httpTransportSE = HttpTransportSE(Utils.SOAP_URL)
try {
httpTransportSE.call(SOAP_ACTION, envelope)
val soapPrimitive: SoapObject = envelope.response as SoapObject
// index 1 is Stopdata.
var data: SoapObject = soapPrimitive.getProperty(1) as SoapObject
// get to "Document Element"
if(data.hasProperty("DocumentElement")){
var documentElement: SoapObject = data.getProperty("DocumentElement") as SoapObject
for(i in 1..documentElement.propertyCount) {
var obj = documentElement.getProperty(i-1) as SoapObject
var bus = Bus()
bus.MonitoredStopVisit_MonitoringRef = obj.getProperty("MonitoredStopVisit_MonitoringRef").toString()
bus.MonitoredVehicleJourney_LineRef = obj.getProperty("MonitoredVehicleJourney_LineRef").toString()
bus.MonitoredVehicleJourney_DirectionRef = obj.getProperty("MonitoredVehicleJourney_DirectionRef").toString()
bus.MonitoredVehicleJourney_PublishedLineName = obj.getProperty("MonitoredVehicleJourney_PublishedLineName").toString()
bus.MonitoredVehicleJourney_DestinationRef = obj.getProperty("MonitoredVehicleJourney_DirectionRef").toString()
bus.MonitoredVehicleJourney_DestinationName = obj.getProperty("MonitoredVehicleJourney_DestinationName").toString()
bus.MonitoredCall_ExpectedArrivalTime = obj.getProperty("MonitoredCall_ExpectedArrivalTime").toString()
bus.MonitoredCall_ExpectedDepartureTime = obj.getProperty("MonitoredCall_ExpectedDepartureTime").toString()
busList.add(bus)
}
} else {
Log.v("**DEBUG**","busList size : " + busList.size.toString())
}
} catch (e: Exception) {
e.printStackTrace()
}
return busList
}
公交车类
class Bus {
lateinit var MonitoredStopVisit_MonitoringRef: String
lateinit var MonitoredVehicleJourney_LineRef: String
lateinit var MonitoredVehicleJourney_DirectionRef: String
lateinit var MonitoredVehicleJourney_PublishedLineName: String
lateinit var MonitoredVehicleJourney_DestinationRef: String
lateinit var MonitoredVehicleJourney_DestinationName: String
lateinit var MonitoredCall_ExpectedArrivalTime: String
lateinit var MonitoredCall_ExpectedDepartureTime: String
}