实际上是什么问题,我向Web服务Web服务发出了POST请求并收到了响应
要求
--> POST https://www.dataaccess.com/webservicesserver/NumberConversion.wso
Content-Type: text/xml
Content-Length: 247
Accept-Charset: utf-8
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
<ubiNum>1000</ubiNum>
</NumberToWords>
</soap:Body>
</soap:Envelope>
--> END POST (247-byte body)
回答
<-- 200 https://www.dataaccess.com/webservicesserver/NumberConversion.wso (1333ms)
cache-control: private, max-age=0
content-type: text/xml; charset=utf-8
vary: Accept-Encoding
server: Server
web-service: DataFlex 19.1
x-powered-by: ASP.NET
date: Thu, 02 Nov 2023 15:52:53 GMT
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<m:NumberToWordsResponse xmlns:m="http://www.dataaccess.com/webservicesserver/">
<m:NumberToWordsResult>one thousand</m:NumberToWordsResult>
</m:NumberToWordsResponse>
</soap:Body>
</soap:Envelope>
<-- END HTTP (344-byte body)
从这个答案中我想得到“一千”我该怎么做?
代码接口
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST
interface soapAPI {
@Headers(
"Content-Type: text/xml",
"Accept-Charset: utf-8"
)
@POST("webservicesserver/NumberConversion.wso")
suspend fun sendApi(@Body envelope: SoapEnvelope): Response<output>
}
请求代码
import org.simpleframework.xml.Element
import org.simpleframework.xml.Namespace
import org.simpleframework.xml.Root
@Namespace(prefix = "soap", reference = "http://schemas.xmlsoap.org/soap/envelope/")
@Root(name = "Envelope")
data class SoapEnvelope(
@field:Element(name = "soap:Body", required = false)
var body: SoapBody? = null
)
@Root(name = "soap:Body", strict = false)
data class SoapBody(
@field:Element(name = "NumberToWords")
var numberToWords: NumberToWords? = null
)
@Root(name = "NumberToWords", strict = false)
@Namespace(reference = "http://www.dataaccess.com/webservicesserver/")
data class NumberToWords(
@field:Element(name = "ubiNum", required = false)
var ubiNum: String? = null
)
用于获取答案的数据类(可能这是我的代码的弱点)
data class output(
val ubiNum: String
)
改造2、okHttp3、simpleXML
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.addInterceptor(interceptor)
.build()
val retrofit = Retrofit.Builder()
.baseUrl("https://www.dataaccess.com/")
.client(client)
.addConverterFactory(SimpleXmlConverterFactory.create())
.build()
val productApi = retrofit.create(soapAPI::class.java)
CoroutineScope(Dispatchers.IO).launch {
val envelope = SoapEnvelope(
body = SoapBody(
numberToWords = NumberToWords("1000")
)
)
Log.d("check", "перед ошибкой")
val response = productApi.sendApi(envelope)
Log.d("check", "после ошибки") //это не выполняется
if (response.isSuccessful){
val output = response.body()
Log.d("check", "${output}")
}
else {
val errorBody = response.errorBody()
Log.d("check", "ошибка")// выполняется если неправильный запрос
}
}
当代码 200 发生致命错误并且应用程序崩溃时
FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.myapplication, PID: 10024
java.lang.RuntimeException: org.simpleframework.xml.core.ElementException: Element 'Body' does not have a match in class com.example.myapplication.output at line -1
at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:45)
at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:23)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@a4ec63, Dispatchers.IO]
Caused by: org.simpleframework.xml.core.ElementException: Element 'Body' does not have a match in class com.example.myapplication.output at line -1
at org.simpleframework.xml.core.Composite.readElement(Composite.java:527)
at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
at org.simpleframework.xml.core.Composite$Injector.read(Composite.java:1433)
at org.simpleframework.xml.core.Composite.read(Composite.java:201)
at org.simpleframework.xml.core.Composite.read(Composite.java:148)
at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
at org.simpleframework.xml.core.Persister.read(Persister.java:625)
at org.simpleframework.xml.core.Persister.read(Persister.java:606)
at org.simpleframework.xml.core.Persister.read(Persister.java:584)
at org.simpleframework.xml.core.Persister.read(Persister.java:562)
at retrofit2.converter.simplexml.SimpleXmlResponseBodyConverter.convert(SimpleXmlResponseBodyConverter.java:37)
不要严格评判,我是第一次做这样的事情,很多东西还是不明白,还想请教一下哪里可以得到这方面的资料,谢谢。