Android使用Retrofit进行SOAP通信
Jun 19, 2017
Retrofit是当前使用最为广泛的Android网络请求框架,它可以构建不同的网络请求格式进行网络请求,使用Retrofit我们可以根据SOAP协议构建网络请求,此次记录一次完整的构建过程。
在前面的文章在Android中使用SOAP通信写了SOAP协议,其本质上是XML+HTTP,使用HTTP协议进行XML格式数据的通信。如果一个客户端向不同的后台服务器进行数据通信时,多网络框架的使用是非常不便的。
Retrofit是当前使用最为广泛的Android网络请求框架,它可以构建不同的网络请求格式进行网络请求,使用Retrofit我们可以根据SOAP协议构建网络请求,此次记录一次完整的构建过程。
一、Retrofit框架引入
主要是Retrofit和OkHttp框架。
|
|
二、请求构建
获取SOAP请求结构
首先使用抓包工具对网络请求进行抓包(抓包的教程可以百度),获取数据包,然后使用postman进行数据调试。下面是我的调试成功数据:
请求数据
返回数据
使用simplexml构建请求
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869/*** 1.RequestEnvelope构建*/"soapenv:Envelope")(name =({"http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),(reference ="http://www.w3.org/2001/XMLSchema", prefix = "xsd"),(reference ="http://schemas.xmlsoap.org/soap/envelope/", prefix = "soapenv")(reference =})public class RequestEnvelope {"soapenv:Body", required = false)(name =public RequestBody requestBody;}"soapenv:Body", strict = false)(name =public class RequestBody {"GetCommand", required = false)(name =public RequestModel GetCommand;}public class RequestModel {"xmlns")(name =public String Attribute;"E_SN", required = false)(name =public String E_SN;}/**2.ResponseEnvelope构建*/"soapnev:Envelope")(name =({"http://schemas.xmlsoap.org/soap/envelope/", prefix = "soapenv"),(reference ="http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),(reference ="http://www.w3.org/2001/XMLSchema", prefix = "xsd")(reference =})public class ResponseEnvelope {"Body")(name =public ResponseBody responseBody;}"Body")(name =public class ResponseBody {"GetCommandResponse",required = false)(name =public GetCommandResponse getCommandResponse;}"GetCommandResponse")(name ="xmlns",empty = "http://mobiletone.cn/")(name =public class GetCommandResponse {"GetCommandResult",required = false)(name =public String GetCommandResult;}/*** 3.API接口编写*/({"Content-Type:text/xml;charset=utf-8","SOAPAction:http://*********"})"WEBSERVICE/Services.asmx")(Observable<ResponseEnvelope> getCommand(@Body RequestEnvelope requestEnvelope);PS : 在构建RequestEnvelope时,不同的SOAP协议版本(该次请求是VER11)@NamespaceList头部是有差异的,需要根据调试信息调整。
发送请求,并解析返回值
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677/*** 1.Retrofit初始化*/public class RetrofitGenerator {private static OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();private static final String BASE_URL = "*********";private static Strategy strategy = new AnnotationStrategy();private static Serializer serializer = new Persister(strategy);private static ServerRequest serviceRequest;private static Retrofit.Builder retrofitBuilder = new Retrofit.Builder().addConverterFactory(SimpleXmlConverterFactory.create(serializer)).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl(BASE_URL);private static <S> S createRetrofit(Class<S> cls) {okHttpBuilder.interceptors().add(new Interceptor() {public okhttp3.Response intercept(Chain chain) throws IOException {Request original = chain.request();Request.Builder requestBuilder = original.newBuilder().header("Content-Type", "text/xml;charset=UTF-8").method(original.method(), original.body());Request request = requestBuilder.build();return chain.proceed(request);}});OkHttpClient client = okHttpBuilder.connectTimeout(2, TimeUnit.MINUTES).writeTimeout(2, TimeUnit.MINUTES).readTimeout(2, TimeUnit.MINUTES).build();Retrofit retrofit = retrofitBuilder.client(client).build();return retrofit.create(cls);}public static ServerRequest getServiceStore() {if (serviceRequest == null) {serviceRequest = createRetrofit(ServerRequest.class);}return serviceRequest;}}/*** 2.参数填写*/public class RequestConstruct {public static Observable<ResponseEnvelope> getCommand() {RequestEnvelope requestEnvelop = new RequestEnvelope();RequestBody requestBody = new RequestBody();RequestModel requestModel = new RequestModel();requestModel.Attribute = NAME_SPACE;requestModel.E_SN = IConfig.E_SN_VALUE;requestBody.GetCommand = requestModel;requestEnvelop.requestBody = requestBody;return RetrofitGenerator.getServiceStore().getCommand(requestEnvelop);}}/*** 3.订阅获取返回值*/public void subscribe(){RequestConstruct.getCommand().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ResponseEnvelope>() {public void call(ResponseEnvelope responseEnvelope) {String result =responseEnvelope.responseBody.getCommandResponse.GetCommandResult;}});}