Android Studio 3.4
我正在测试HolidayService2端点,我想通过改造使用该端点。
这是请求端点:
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>
和响应端点:
<?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>
我为请求创建的类是:
@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)
对于响应类,我做了以下工作:
@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?)
这是我第一次使用Web服务并解析xml。但是,我不确定我设置的类是否可以反映xml。
这是我的终点
interface WebServices {
@Headers("Content-Type: text/xml")
@POST("/HolidayService_v2/HolidayService2.asmx")
fun getAvailableCountries(@Body countriesAvailableRequest: CountriesAvailableRequestEnvelope): Observable<CountriesAvailableResponseEnvelope>
}
用于使用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)
}
})
}
我的改装设置如下:
@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()
}
不确定这是否是原因,但是我在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
非常感谢您的任何建议
答案 0 :(得分:4)
您又缺少一门课程来容纳Body
来请求。
即Envelope
将包含Body
对象,并且
Body
将包含GetCountriesAvailable
个对象。
示例:
@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)
@Namespace(reference="http://www.w3.org/2003/05/soap-envelope", prefix:"soap12")
val Body: Body)
@Root(name = "Body", strict = false)
@Namespace(reference="http://www.w3.org/2003/05/soap-envelope", prefix:"soap12")
data class Body(
@Element(name = "GetCountriesAvailable", required = false)
val countriesAvailableRequest: CountriesAvailableRequest)
@Root(name = "GetCountriesAvailable", strict = false)
@Namespace(reference = "http://www.holidaywebservice.com")
data class CountriesAvailableRequest(
@Element(name = "GetCountriesAvailable", required = false) val code: String)
答案 1 :(得分:2)
请勿在元素名称中包含@Root(name = "soap12: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")
])
。
更改
@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")
])
到
Body
对于@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?)
元素更改如下。
$conf['sentmail']['params']['limit_period']
答案 2 :(得分:1)
请求
@Root(name = "soap: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 = "soap", reference = "http://www.w3.org/2003/05/soap-envelope")
]
)
class CountriesAvailableRequestEnvelope(
@field:Path("soap:Body")
@field:Element(name ="GetCountriesAvailable")
@param:Element(name ="GetCountriesAvailable")
@field:Namespace(reference = "http://www.holidaywebservice.com/HolidayService_v2/")
val getCountries: String = ""
)
使用
val result = webServices.getAvailableCountries(CountriesAvailableRequestEnvelope())
...
响应
@Root(name = "soap: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 = "soap", reference = "http://www.w3.org/2003/05/soap-envelope")]
)
class CountriesAvailableResponseEnvelope {
@field:Path("soap:Body")
@field:Element(name = "GetCountriesAvailableResponse")
@field:Namespace(reference = "http://www.holidaywebservice.com/HolidayService_v2/")
var response: GetCountriesAvailableResponse? = null
}
@Element
class GetCountriesAvailableResponse {
var GetCountriesAvailableResult: List<CountryCode>? = null
}
class CountryCode {
@field:Element(name = "Description")
var description: String? = null
@field:Element(name = "Code")
var code: String? = null
}
logcat
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: --> POST http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: Content-Type: text/xml
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: Content-Length: 329
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: <soap:Body>
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: <GetCountriesAvailable xmlns="http://www.holidaywebservice.com/HolidayService_v2/"></GetCountriesAvailable>
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: </soap:Body>
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp: </soap:Envelope>
2019-04-08 12:00:23.990 22722-22748/nz.org.westforce.mobileui D/OkHttp: --> END POST (329-byte body)
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: <-- 200 OK http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx (372ms)
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: Cache-Control: private, max-age=0
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: Content-Type: application/soap+xml; charset=utf-8
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: Server: Microsoft-IIS/8.5
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: X-AspNet-Version: 4.0.30319
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: X-Powered-By: ASP.NET
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: X-Powered-By-Plesk: PleskWin
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: Date: Mon, 08 Apr 2019 03:48:45 GMT
2019-04-08 12:00:24.363 22722-22748/nz.org.westforce.mobileui D/OkHttp: Content-Length: 983
2019-04-08 12:00:24.368 22722-22748/nz.org.westforce.mobileui D/OkHttp: <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetCountriesAvailableResponse xmlns="http://www.holidaywebservice.com/HolidayService_v2/"><GetCountriesAvailableResult><CountryCode><Code>Canada</Code><Description>Canada</Description></CountryCode><CountryCode><Code>GreatBritain</Code><Description>Great Britain and Wales</Description></CountryCode><CountryCode><Code>IrelandNorthern</Code><Description>Northern Ireland</Description></CountryCode><CountryCode><Code>IrelandRepublicOf</Code><Description>Republic of Ireland</Description></CountryCode><CountryCode><Code>Scotland</Code><Description>Scotland</Description></CountryCode><CountryCode><Code>UnitedStates</Code><Description>United States</Description></CountryCode></GetCountriesAvailableResult></GetCountriesAvailableResponse></soap:Body></soap:Envelope>
2019-04-08 12:00:24.369 22722-22748/nz.org.westforce.mobileui D/OkHttp: <-- END HTTP (983-byte body)