【发布时间】:2018-12-27 10:48:13
【问题描述】:
我需要在 kotlin 中解析这个响应 XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.isban.es/webservices/TECHNICAL_FACADES/Security/F_facseg_security/internet/loginServicesNSegSAN/v1">
<soapenv:Header/>
<soapenv:Body>
<v1:authenticateCredential facade="loginServicesNSegSAN">
<CB_AuthenticationData>
<password>12313212</password>
<documento>
<TIPO_DOCUM_PERSONA_CORP>N</TIPO_DOCUM_PERSONA_CORP>
<CODIGO_DOCUM_PERSONA_CORP>12312321d</CODIGO_DOCUM_PERSONA_CORP>
</documento>
</CB_AuthenticationData>
<!--<userAddress>182...</userAddress>-->
</v1:authenticateCredential>
</soapenv:Body>
</soapenv:Envelope>
这是我的响应映射类
@Root(name = "soap-env:Envelope", strict = false)
@Namespace(prefix = "soap-env", reference "http://schemas.xmlsoap.org/soap/envelope/")
class Envelope {
@field:Element(name = "soap-env:Body", required = false)
var body: Body? = null
}
@Root(name = "soap-env:Header", strict = false)
class Header {
}
@Root(name = "soap-env:Body", strict = false)
class Body {
@field:Element(name = "prefixRigel0:authenticateCredentialResponse", required = false)
@Namespace(prefix = "prefixRigel0", reference = "http://www....")
var credResponse: CredentialResponse? = null
}
@Root(name = "prefixRigel0:authenticateCredentialResponse", strict = false)
@Namespace(prefix = "prefixRigel0", reference = "http://www....")
class CredentialResponse {
@field:Element(name = "methodResult", required = false)
var methodResponse: MethodResult? = null
}
@Root(name = "methodResult", strict = false)
class MethodResult {
@field:Element(name = "tokenCredential", required = false)
var token: String? = null
}
我读过一些类似Kotlin 1.2.21 + SimpleXml 2.3.0 - consume List error (must mark set get method) 和Parsing Soap Service use of Retrofit I am facing exception like this(i.e)mismatched body model in the class envelope model in the response 的帖子
这是我的改造请求
val requestBodyText = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://www....\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <v1:authenticateCredential facade=\"login...\">\n" +
" <CB_AuthenticationData>\n" +
" <password>1111</password>\n" +
" <documento>\n" +
" <TIPO_DOCUM_PERSONA_CORP>N</TIPO_DOCUM_PERSONA_CORP>\n" +
" <CODIGO_DOCUM_PERSONA_CORP>1111H</CODIGO_DOCUM_PERSONA_CORP>\n" +
" </documento>\n" +
" </CB_AuthenticationData>\n" +
" <!--<userAddress>180...</userAddress>-->\n" +
" </v1:authenticateCredential>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>"
val requestBody = RequestBody.create(MediaType.parse("text/xml"), requestBodyText)
val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder().baseUrl("https://www...").addConverterFactory(SimpleXmlConverterFactory.create()).build()
val api = retrofit.create(ServicesSoap::class.java!!)
var call: Call<Envelope>? = null
call = api.getToken(requestBody)
if (call != null) {
call!!.enqueue(object : Callback<Envelope> {
override fun onResponse(call: Call<Envelope>, response: Response<Envelope>) {
if (response.body() != null) {
Log.v("Retrofit call ", "response: " + response.isSuccessful() + " Message: " + response.message())
var v = response.body()
}
}
override fun onFailure(call: Call<Envelope>, t: Throwable) {
var v = "" + t.localizedMessage + " - " + t.message
}
})
}
}
我得到了一个 200 OK 响应,body = null
编辑 我也尝试过使用@get 而不是@field,但也没有工作 Retrofit2 + SimpleXML in Kotlin: MethodException: Annotation must mark a set or get method 并更改为带有 Parsing xml kotlin android 中解释的参数的数据类,但也无法正常工作
我已经尝试过这样的 Java 类,但也无法正常工作
@Root(name = "soap-env:Envelope", strict = false)
@Namespace(prefix = "soap-env", reference = "http://schemas.xmlsoap.org/soap/envelope/")
public class Envelope {
@Element(name = "soap-env:Body", required = false)
Body body;
}
@Root(name = "soap-env:Header", strict = false)
class Header {
}
@Root(name = "soap-env:Body", strict = true)
class Body {
@Element(name = "prefixRigel0:authenticateCredentialResponse", required = false)
@Namespace(prefix = "prefixRigel0", reference = "http://www....")
CredentialResponse credResponse;
}
@Root(name = "prefixRigel0:authenticateCredentialResponse", strict = true)
@Namespace(prefix = "prefixRigel0", reference = "http://www....")
class CredentialResponse {
@Element(name = "methodResult", required = false)
MethodResult methodResponse;
}
@Root(name = "methodResult", strict = true)
class MethodResult {
@Element(name = "tokenCredential", required = false)
String token;
}
感谢您的帮助!
【问题讨论】:
-
你试过用Java定义类吗?当我使用 SimpleXML 时,无论出于何种原因,Java 类都可以正常工作,而 Kotlin 则不能。
-
我试过了,但还是不行。我发布我的 java 类,看看它是否错误
标签: android soap kotlin retrofit2