Retrofit2+Okhttp3+Rxjava2通过SOAP协议请求WebService
刚入职新公司不久,花了九牛二虎把公司源码阅读了一遍,用两个字形容就是:”卧槽”,这写的啥玩意啊,和领导商议之后打算用空余时间重构一下源码,其中主要是网络请求最为频繁之一(感觉是在说废话),发现后台是WebService,以前都是JAVA的,这没弄过啊,经过查阅之后可以用Ksoap2来进行请求,发现Ksoap2挺麻烦的,完全没有之前okhttp+Retrofit那么容易,所以简单的封装了一下基于Retrofit2+Okhttp3+Rxjava2的一个SOAP协议请求库。
第三方依赖
//retrofit依赖
compile 'com.squareup.retrofit2:retrofit:2.2.0'
//XML解析
compile('com.squareup.retrofit2:converter-simplexml:2.2.0') {
exclude group: 'xpp3', module: 'xpp3'
exclude group: 'stax', module: 'stax-api'
exclude group: 'stax', module: 'stax'
}
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.squareup.retrofit2:converter-scalars:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
//OkHttp依赖
compile 'com.squareup.okhttp3:okhttp:3.7.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.7.0'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.7.0'
//RxJava依赖
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex.rxjava2:rxjava:2.0.9'
请求体RequestEnvelope
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.NamespaceList;
import org.simpleframework.xml.Root;
@Root(name = "soap:Envelope")
@NamespaceList({
@Namespace(reference = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),
@Namespace(reference = "http://www.w3.org/2001/XMLSchema", prefix = "xsd"),
@Namespace(reference = "http://schemas.xmlsoap.org/soap/envelope/", prefix = "soap")
})
public class RequestEnvelope {
@Element(name = "soap:Body", required = false)
public RequestBody body;
public RequestEnvelope(RequestBody body) {
this.body = body;
}
}
请求体RequestBody
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name = "soap:Body", strict = false)
public class RequestBody {
@Element(name = "Move_GetDate", required = false)
public RequestModel AssetMaterialInfo;
public RequestBody(RequestModel assetMaterialInfo) {
AssetMaterialInfo = assetMaterialInfo;
}
}
请求体RequestModel
import org.simple