前言
之前写了一篇文章 Retrofit2+Okhttp3+Rxjava通过SOAP协议请求WebService 通过整合Retrofit,Okhttp,Rxjava来实现WebService的网络访问,但其中还有许多地方不够完善,并没有发挥出Rxjava的强大之处,因此在这里通过完善封装,加入网络请求的取消,避免内存泄漏,使之更加简洁,方便使用。
这里只呈现出修改变动的部分,不太明白的话可以先看之前的文章。
依赖
Rxjava使用2.0的(之前使用1.0)
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
新增Rxjava与Retrofit的适配器(Square官方提供的适配器目前仅支持Rxjava1)
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
内容
1.ServiceStore请求接口
使用了Rxjava与Retrofit的适配器,因此,返回类型变为Observable<ResponseBody>
public interface ServiceStore {
@Headers({
"Content-Type: text/xml; charset=utf-8",
"SOAPAction: http://tempuri.org/NewsInquiry"
})
@POST("WebServices/EhomeWebservice.asmx")
Observable<ResponseBody> getNews(@retrofit2.http.Body String str);
@GET
Observable<ResponseBody> download(@Url String fileUrl);
@GET("http://gank.io/api/data/{type}/{pageSize}/{pageIndex}")
Observable<ResultsEntity> getInfo(@Path("type") String type, @Path("pageSize") String pageSize, @Path("pageIndex") String pageIndex);
}
2.RequestManager请求管理者
移除execute()和doRequest()方法,对Retrofit进行初始化配置,ServiceStore的创建移到RequestManager初始化,这就意味着所有的请求接口都要写在ServiceStore内。
public class RequestManager {
public final static int CONNECT_TIMEOUT = 10;
public final static int READ_TIMEOUT = 20;
public final static int WRITE_TIMEOUT = 10;
public Retrofit mRetrofit;
private static RequestManager mRequestManager;//管理者实例
public OkHttpClient mClient;//OkHttpClient实例
public ServiceStore mServiceStore;//请求接口
private RequestManager() {
init();
}
//单例模式,对提供管理者实例
public static RequestManager getInstance() {
if (mRequestManager == null) {
synchronized (RequestManager.class) {
if (mRequestManager == null) {
mRequestManager = new RequestManager();
}
}
}
return mRequestManager;
}
private void init(){
Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister(strategy);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS);
builder.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS);
builder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS);
builder.retryOnConnectionFailure(true);
mClient = builder.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(Constans.WEBSERVICE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(SimpleXmlConverterFactory.create(serializer))
.client(mClient)
.build();
mServiceStore=mRetrofit.create(ServiceStore.class);
}
public interface onRequestCallBack{
void onSuccess(String msg);
void onError(String msg);
}
}
3.新增ResultObserver类
该类实现Observer接口,对上游事件(请求结果)进行解析,然后通过自定义接口进行数据回调
public class ResultObserver implements Observer<ResponseBody> {
private RequestManager.onRequestCallBack callBack;
public ResultObserver( RequestManager.onRequestCallBack callBack){
this.callBack=callBack;
}
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull ResponseBody responseBody) {
try {
String res = responseBody.string();
if (res == null) {
callBack.onError("请求发生未知错误");
return;
}
if (res.contains("{") && res.contains("}")) {
int startIndex = res.indexOf("{");
int endIndex = res.lastIndexOf("}") + 1;
String json = res.substring(startIndex, endIndex);
callBack.onSuccess(json);
}
} catch (IOException e) {
e.printStackTrace();
callBack.onError(e.toString());
}
}
@Override
public void onError(@NonNull Throwable e) {
callBack.onError(e.toString());
}
@Override
public void onComplete() {
}
}
4.请求参数拼装
public class Node {
//"<"节点转义
private static String toStart(String name){
return "<"+name+">";
}
//">"节点转义
private static String toEnd(String name){
return "</"+name+">";
}
//请求参数拼接
public static String getRequestParams(String namespace, Map<String,String> map){
if(map==null){
map=new HashMap<>();
}
StringBuffer sbf=new StringBuffer();
sbf.append(Node.toStart("Request"));
for(Map.Entry<String,String> entry:map.entrySet()){
sbf.append(Node.toStart(entry.getKey()));
sbf.append(entry.getValue());
sbf.append(Node.toEnd(entry.getKey()));
}
sbf.append(Node.toEnd("Request"));
String str="<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Header>\n" +
" <Identify xmlns=\"http://tempuri.org/\">\n" +
" <UserName>"+ Constans.USERNAME+"</UserName>\n" +
" <PassWord>"+Constans.PASSWORD+"</PassWord>\n" +
" </Identify>\n" +
" </soap:Header>\n" +
" <soap:Body>\n" +
" <"+namespace+" xmlns=\"http://tempuri.org/\">\n" +
" <str>"+sbf.toString()+"</str>\n" +
" </"+namespace+">\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
return str;
}
}
5.请求示例
Rxjava2会返回一个 Disposable,它是观察者与被观察者之间的一个开关,调用它的dispose()方法,下游将不会收到发送事件,可以在页面销毁时调用,避免执行更新UI。
CompositeDisposable是Rxjava内置的一个容器,用于保存 Disposable,在页面销毁时候调用它的clear方法即可
CompositeDisposable mCompositeDisposable= new CompositeDisposable();
private void getNews() {
Map<String,String> map=new HashMap<>();
map.put("PageSize",10+"");
map.put("PageIndex",1+"");
String request= Node.getRequestParams("NewsInquiry",map);
Disposable disposable = RequestManager.getInstance()
.mServiceStore
.getNews(request)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new ResultObserver(new RequestManager.onRequestCallBack() {
@Override
public void onSuccess(String msg) {
tv_msg.setText(msg);
}
@Override
public void onError(String msg) {
tv_msg.setText(msg);
}
}));
mCompositeDisposable.add(imageDisposable);
}
@Override
protected void onDestroy() {
super.onDestroy();
mCompositeDisposable.clear();
}
6.点击“网络请求”按钮,结果如图