我需要一些帮助来实现使用RxJava2的并行异步调用& Retrofit2。 我的要求是;如何使用RxJava2和Retrofit2创建并行多个非阻塞服务请求
1)我有多个保险公司(现在我只需要两个),我需要发送多个使用该保险公司名称的并行请求。
2)如果它们中的任何一个给服务器错误,那么其余的请求不应该被阻塞。
以下是我到现在为止所尝试的;
ArrayList<String> arrInsurer = new ArrayList<>();
arrInsurer.add(AppConstant.HDFC);
arrInsurer.add(AppConstant.ITGI);
RequestInterface service = getService(ServiceAPI.CAR_BASE_URL);
for (String insurerName : arrInsurer) {
service.viewQuote(Utils.getPrefQuoteId(QuoteListActivity.this), insurerName)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ViewQuoteResDTO>() {
@Override
public void accept(@NonNull ViewQuoteResDTO viewQuoteResDTO) throws Exception {
Log.e("Demo", viewQuoteResDTO.getPremiumData().getIDV()+"");
updateList();
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
Log.e("Demo", throwable.getMessage());
}
});
}
private RequestInterface getService(String baseUrl) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
return new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build().create(RequestInterface.class);
}
现在,上面的代码只有在两个请求都能成功响应的情况下才能正常工作。但是当任何请求作为内部服务器错误发出响应时,请求的其余部分也会被阻塞。
下面的任何一个请求给出的日志错误我得到失败响应;
E/Demo: HTTP 500 Aww Snap, Some thing happened at server. Please try back again later.
E/Demo: unexpected end of stream on Connection{100.xxx.xxx.xx:portNo, [email protected] hostAddress=/100.xxx.xxx.xx:portNo cipherSuite=none protocol=http/1.1}
如何处理这个错误?