使用改造参数 "soap12:Body" 使用 Xml Web 服务在类中没有匹配项

共3个回答, 标签: android web-services soap retrofit2
Android Studio 3.4

我正在测试假日服务 2 终结点, 我想使用改造来使用该终结点。

这是请求终结点:

POST /HolidayService_v2/HolidayService2.asmx HTTP/1.1
Host: www.holidaywebservice.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length






和响应终结点:

我为请求创建的类是:

@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
}

对于使用 rxJava 调用终结点:

fun requestFromWebService() {    webServices.getAvailableCountries(CountriesAvailableRequestEnvelope(CountriesAvailableRequest("UK")))
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.trampoline())
            .subscribeWith(object : Observer {
                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()
    }

不确定这是否是

第1个答案

您缺少一个要保留请求的类. 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)
第2个答案

不要 prefix 作为元素名称的一部分包括。

改变

@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?)
第3个答案

请求

@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())
...

洛卡特

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:
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp:
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp:
2019-04-08 12:00:23.989 22722-22748/nz.org.westforce.mobileui D/OkHttp:
2019-04-08 12:00:23.990 22722-22748/nz.org.westforce.mobileui D/OkHttp: --> END POST (329-byte body)
<-- 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:="">CanadaCanadaGreatBritainGreat Britain and WalesIrelandNorthernNorthern IrelandIrelandRepublicOfRepublic of IrelandScotlandScotlandUnitedStatesUnited States
2019-04-

相关问题

无法通过新项目从 jcenter 获取依赖项 [已关闭] 注意: 无法读取获取 [Ljava.lang.Object;@79d6c4df 的 kotlin 元数据 我的单身可以被多次调用 使用改造参数 "soap12:Body" 使用 Xml Web 服务在类中没有匹配项 JSON 响应对于 int 值总是返回零 为什么我不能从 json Retrofit 中获取特殊字段?