Собственно в чем проблема, я сделал 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)
из этого ответа я бы хотел достать "one thousand" Как мне это сделать?
код интерфейс
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
)
retrofit2, 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)
Не судите строго, занимаюсь подобным первый раз и много чего еще не понимаю, также хочу попросить совета где набрать информации по данной теме, спасибо.
SoapEnvelope
для запроса, так же и ответ распишите. А то бац, иResponse<output>
какой-то, да и еще с маленькой буквы :)