Android Studio 3.4
I am testing the HolidayService2 endpoint and I want to consume that endpoint using retrofit.
This is the request endpoint:
POST /HolidayService_v2/HolidayService2.asmx HTTP/1.1
Host: www.holidaywebservice.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetCountriesAvailable xmlns="http://www.holidaywebservice.com/HolidayService_v2/" />
</soap12:Body>
and the response endpoint:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetCountriesAvailableResponse xmlns="http://www.holidaywebservice.com/HolidayService_v2/">
<GetCountriesAvailableResult>
<CountryCode />
<CountryCode />
</GetCountriesAvailableResult>
</GetCountriesAvailableResponse>
</soap12:Body>
</soap12:Envelope>
The classes I have created for the request is:
@Root(name = "Envelope")
@NamespaceList(value = [
Namespace(prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance"),
Namespace(prefix = "xsd", reference = "http://www.w3.org/2001/XMLSchema"),
Namespace(prefix = "soap12", reference = "http://www.w3.org/2003/05/soap-envelope")
])
data class CountriesAvailableRequestEnvelope(
@Element(name = "Body", required = false) val countriesAvailableRequest: CountriesAvailableRequest)
@Root(name = "GetCountriesAvailable", strict = false)
@NamespaceList(value = [
Namespace(reference = "http://www.holidaywebservice.com"),
Namespace(reference="http://www.w3.org/2003/05/soap-envelope")])
data class CountriesAvailableRequest(
@Element(name = "GetCountriesAvailable", required = false) val code: String)
And for the response classes I have done the following:
@Root(name = "Envelope")
@NamespaceList(value = [
Namespace(prefix = "xsi", reference = "http://www.w3.org/2001/XMLSchema-instance"),
Namespace(prefix = "xsd", reference = " http://www.w3.org/2001/XMLSchema"),
Namespace(prefix = "soap12", reference = "http://www.w3.org/2003/05/soap-envelope")])
data class CountriesAvailableResponseEnvelope(
@Element(required = false, name = "Body") val countryCode: CountryCode)
@Root(name = "Body", strict = false)
@Namespace(reference="http://www.w3.org/2003/05/soap-envelope")
data class CountryCode(
@Element(name = "description", required = false) val description: String?,
@Element(name = "code", required = false) val code: String?)
This is the first time I am consuming web service and parsing xml. However, I am not sure if my classes I have setup reflect the xml.
This is my endpoint:
interface WebServices {
@Headers("Content-Type: text/xml")
@POST("/HolidayService_v2/HolidayService2.asmx")
fun getAvailableCountries(@Body countriesAvailableRequest: CountriesAvailableRequestEnvelope): Observable<CountriesAvailableResponseEnvelope>
}
For calling the endpoint using rxJava:
fun requestFromWebService() { webServices.getAvailableCountries(CountriesAvailableRequestEnvelope(CountriesAvailableRequest("UK")))
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.trampoline())
.subscribeWith(object : Observer<CountriesAvailableResponseEnvelope> {
override fun onComplete() {
println("onComplete")
}
override fun onNext(t: CountriesAvailableResponseEnvelope) {
println(t.countryCode)
}
override fun onError(e: Throwable) {
println(e.message)
}
})
}
My retrofit setup looks like this:
@Reusable
@Provides
fun provideRetrofit(context: Context, okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(http://www.holidaywebservice.com)
.client(okHttpClient)
.addConverterFactory(SimpleXmlConverterFactory.createNonStrict(Persister(AnnotationStrategy())))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
Not sure if this is the reason why but I get this message in OnError
:
I/System.out: org.simpleframework.xml.core.ConstructorException: Parameter 'soap12:Body' does not have a match in class nz.org.westforce.data.entities.holidaytest.CountriesAvailableRequestEnvelope
Many thanks for any suggestions