Created
September 3, 2019 06:46
-
-
Save iponkan/faeb62f73528a7c968f88a9cfb77e06a to your computer and use it in GitHub Desktop.
嵌套网络请求处理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
skyNet.getPublicKey(System.currentTimeMillis()) | |
.compose(RxSchedulers.io_main()) | |
.observeOn(Schedulers.io()) // (新被观察者,同时也是新观察者)切换到IO线程去发起登录请求 | |
// 特别注意:因为flatMap是对初始被观察者作变换,所以对于旧被观察者,它是新观察者,所以通过observeOn切换线程 | |
// 但对于初始观察者,它则是新的被观察者 | |
.flatMap(new Function<PublicKeyBean, ObservableSource<LoginResultBean>>() { // 作变换,即作嵌套网络请求 | |
@Override | |
public ObservableSource<LoginResultBean> apply(PublicKeyBean result) throws Exception { | |
// 将网络请求1转换成网络请求2,即发送网络请求2 | |
if (result != null) { | |
String modulus = result.getModulus(); | |
String exponent = result.getExponent(); | |
String cryPasswd = CrypUtil.encodePass(passwd, modulus, exponent); | |
return skyNet.login(userName, cryPasswd, DeviceUuidFactory.getDeviceUuid()); | |
} else { | |
throw new Exception("getPublicKey Fail"); | |
} | |
} | |
}) | |
.observeOn(AndroidSchedulers.mainThread()) // (初始观察者)切换到主线程 处理网络请求2的结果 | |
.subscribeWith(new RxDataObserver<LoginResultBean>() { | |
@Override | |
public void onSuccess(LoginResultBean model) { | |
if (model != null && model.getResult() == 1) { | |
callBack.onLogin(true); | |
login = true; | |
} else { | |
callBack.onLogin(false); | |
} | |
} | |
@Override | |
public void onFailure(String msg) { | |
LogUtils.e(TAG, "login fail: " + msg); | |
callBack.onLogin(false); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment