当前位置 : 主页 > 网页制作 > Nodejs >

Retrofit进行WebService请求遇到的问题总结

来源:互联网 收集:自由互联 发布时间:2021-06-24
前面写了一篇利用Retrofit进行WebService请求的文章,地址是:http://blog.csdn.net/smileiam/article/details/51957232,下面说说自己在调试过程中,遇到的一些问题及解决办法。给其他遇到类似问题的

前面写了一篇利用Retrofit进行WebService请求的文章,地址是:http://blog.csdn.net/smileiam/article/details/51957232,下面说说自己在调试过程中,遇到的一些问题及解决办法。给其他遇到类似问题的小伙伴,一些参考。


1. soapaction header missing,no SOAPAction header

这个一般是请求的方法,请求头中没有添加SOAPAction,只需要在写请求方法的头上添加上相应的Action

@Headers({"Content-Type: text/xml;charset=UTF-8", "SOAPAction: http://WebXml.com.cn/getWeatherbyCityName"})//请求的Action

2. soapenv:VersionMismatch
这个在stackoverflow上有人给出解释:The faulting node found an invalid element information item instead of the expected Envelope element information item. The namespace, local name or both did not match the Envelope element information item required by this recommendation
大意是因为命名空间写的不对,或是结点元素无效,不是期望的Envelope元素等。
像我是因为粗心,命名空间忘记写最后面的反斜杠。下面是错误的写法:
@Namespace(reference = "http://schemas.xmlsoap.org/soap/envelope", prefix = "env")
正确的写法应该是:
@Namespace(reference = "http://schemas.xmlsoap.org/soap/encoding/", prefix = "enc"),
所以说程序员一定要细心。
3. <faultcode>soapenv:Server.userException</faultcode>
<faultstring>org.xml.sax.SAXParseException: The prefix soapenv for element soapenv:Envelope is not bound.</faultstring>
从报错的原因里,可以看出是因为在命名空间里,没有定义前缀为soapenv这个的空间
这主要是我写命名空间
@Namespace(reference = "http://schemas.xmlsoap.org/soap/envelope/", prefix = "soapenv")的前缀明明写的是soapenv,可是在代码中却用成类似
@Element(name = "env:Body", required = false)
Body前面的前缀应该和prefix里的对应上
4. org.simpleframework.xml.core.ElementException: Element 'Body' does not have a match in class com.combanc.common.communication.webservice.ResponseEnvelope at line 1
这一般是在对Retrofit返回的数据,解析xml时报的错,写的Body的model对应错了
注意Retrofit里的XML解析,用的是SAX解析,每一个标签都应该解析到,要不会报错。属性也需要进行解析的。
网友评论