主要运用了doOnNest 和flatMap进行网络接口嵌套
public class NetPresenter {
private DataCall dataCall;
public NetPresenter(DataCall dataCall) {
this.dataCall = dataCall;
}
public void requestData(Object... args) {
IRequest iRequest = RetrofitUtil.getInstance().create(IRequest.class);
Observable<Result> register = iRequest.Register("15810785404", "123456");
final Observable<Result<UserEnity>> login = iRequest.Login("15810785404", "123456");
final Observable<Result<List<FirstEnity>>> first = iRequest.First();
final Observable<Result<List<SeconedEnity>>> seconed = iRequest.Seconed("1001002");
final Observable<Result<List<FindEnity>>> find = iRequest.Find("1001004002", "1", "10");
// 一级请求
register.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<Result>() {
@Override
public void accept(Result result) throws Exception {
System.out.println("aaaa-reg===" + result.getMessage());
}
})
// 二级请求
.observeOn(Schedulers.io())
.flatMap(new Function<Result, ObservableSource<Result<UserEnity>>>() {
@Override
public ObservableSource<Result<UserEnity>> apply(Result result) throws Exception {
return login;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<Result<UserEnity>>() {
@Override
public void accept(Result<UserEnity> userEnityResult) throws Exception {
UserEnity userEnity = (UserEnity) userEnityResult.getResult();
String userId = userEnity.getUserId();
String sessionId = userEnity.getSessionId();
SpUtil.SavaUserInfo(MyApp.context,userId,sessionId);
System.out.println("aaaa-reg===" + userEnityResult.getMessage());
}
})
// 三级请求
.observeOn(Schedulers.io())// 转换
.flatMap(new Function<Result<UserEnity>, ObservableSource<Result<List<FirstEnity>>>>() {
@Override
public ObservableSource<Result<List<FirstEnity>>> apply(Result<UserEnity> userEnityResult) throws Exception {
return first;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<Result<List<FirstEnity>>>() {
@Override
public void accept(Result<List<FirstEnity>> firstEnityResult) throws Exception {
System.out.println("aaaa-reg===" + firstEnityResult.getMessage());
}
})
// 转换 四级请求
.observeOn(Schedulers.io())
.flatMap(new Function<Result<List<FirstEnity>>, ObservableSource<Result<List<SeconedEnity>>>>() {
@Override
public ObservableSource<Result<List<SeconedEnity>>> apply(Result<List<FirstEnity>> listResult) throws Exception {
return seconed;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<Result<List<SeconedEnity>>>() {
@Override
public void accept(Result<List<SeconedEnity>> seconedEnityResult) throws Exception {
System.out.println("aaaa-er===" + seconedEnityResult.getMessage());
}
})
// 转换 五级
.observeOn(Schedulers.io())
.flatMap(new Function<Result<List<SeconedEnity>>, ObservableSource<Result<List<FindEnity>>>>() {
@Override
public ObservableSource<Result<List<FindEnity>>> apply(Result<List<SeconedEnity>> listResult) throws Exception {
return find;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Result<List<FindEnity>>>() {
@Override
public void accept(Result<List<FindEnity>> listResult) throws Exception {
System.out.println("aaaa-reg===" + listResult.getMessage());
dataCall.success(listResult);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
throwable.printStackTrace();
}
});
}
// 内存泄漏
public void unBind(){
if(dataCall!= null){
dataCall = null;
}
}
}