Okgo使用Rxjava2嵌套请求
flatMap
应用场景:一个接口调用时依赖另一个接口的返回值,如一个接口调用成功后才调用第二个接口或者第二个接口需要第一个接口的返回值作为参数调用
举个栗子下面要实现一个交易,交易步骤是:
1 先从 api拿到节点信息 getEosChainInfo
2 拿到对象后签名
3然后使用api发送给后台pushTransaction
意思是要请求一个接口,本地加工,然后在发动给后台,如何优雅地实现
Api
public static String EOS_BASE_URL = "http://192.168.8.XXX:XX/";
public static String getEosChainInfo = EOS_BASE_URL + "/eos/getChainInfo"; // Eos节点信息
public static String pushTransaction = EOS_BASE_URL + "/eos/pushTransaction"; // push交易
Okgo ServerApi
public class ServerApi {
public static Observable<Response<EosChainInfoModel>> getEosChainInfo(Context context) {
return OkGo.<Response<EosChainInfoModel>>post(HttpModel.getEosChainInfo)
.params("l", CacheUtils.getLanguage(context))
.converter(new JsonConvert<Response<EosChainInfoModel>>() {
})
.adapt(new ObservableBody<Response<EosChainInfoModel>>());
}
public static Observable<Response<EosTxInfo>> pushTransaction(Context context,String pockJson) {
return OkGo.<Response<EosTxInfo>>post(HttpModel.pushTransaction)
.params("code", pockJson)
.params("l", CacheUtils.getLanguage(context))
.converter(new JsonConvert<Response<EosTxInfo>>() {
})
.adapt(new ObservableBody<Response<EosTxInfo>>());
}
}
嵌套请求
ServerApi.getEosChainInfo(params.activity)
.flatMap(new Function<Response<EosChainInfoModel>, Observable<String>>() {
@Override
public Observable<String> apply(Response<EosChainInfoModel> response) throws Exception {
if (response.isCode() && response.getData() != null) {
return signMessage(response.getData(), params);
} else {
if (!TextUtils.isEmpty(response.getMsg())) {
ToastUtils.show(response.getMsg());
}
return Observable.empty();
}
}
}).flatMap(new Function<String, Observable<Response<EosTxInfo>>>() {
@Override
public Observable<Response<EosTxInfo>> apply(String packJson) throws Exception {
if (packJson == null) {
return Observable.empty();
} else {
return ServerApi.pushTransaction(params.activity, packJson);
}
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response<EosTxInfo>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
params.ui.showProgress("","");
}
@Override
public void onNext(@NonNull Response<EosTxInfo> response) {
if (response.isCode() && response.getData() != null) {
//TODO Do something
} else {
String msg = response.getMsg();
if (TextUtils.isEmpty(msg)) {
msg = params.activity.getString(R.string.setting_get_eos_node_erro);
}
if (params.ui.isActive())
DialogController.getInstance().showErrorDialog(params.activity, params.activity.getString(R.string.string_eos_transfer_push_fail), msg);
}
params.wipe();
}
@Override
public void onError(@NonNull Throwable e) {
e.printStackTrace();
params.ui.dismissProgress();
}
@Override
public void onComplete() {
params.ui.dismissProgress();
}
});
}
//签名后返回签名String
private Observable<String> signMessage(EosChainInfoModel infoModel, EosTransferParams params) {
return Observable.just(1)
.map(i -> {
String pockJson = null;
try {
if (params.tx_form == 1) {
pockJson = signTransferMessage(infoModel, params);
} else if (params.tx_form == 2) {
pockJson = createAccountAndSign(infoModel, params);
} else if (params.tx_form == 3) {
pockJson = doSign(infoModel, params);
}
} catch (Exception e) {
e.printStackTrace();
}
return pockJson;
});
}