我正在制作一个汇率应用程序,我的屏幕上有一个图表,显示过去7天内所选货币的变化。
现在我想要的是按照严格的顺序发出物品。
这是我的代码:
public class GraphInteractorImpl implements GraphInteractor {
private final Retrofit retrofit;
@Inject
public GraphInteractorImpl(Retrofit retrofit) {
this.retrofit = retrofit;
}
@Override
public void downloadData(GraphListener listener) {
RestAPI api = retrofit.create(RestAPI.class);
List<String> listDates = getDates();
for (String date : listDates) {
Observable<List<ExchangeRate>> observable = api.getExchangeRatesForLast7days(date);
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
listener::onSuccess,
listener::onFailure
);
}
}
private List<String> getDates() { //returns last 7 days in a list
List<String> listDate = new ArrayList<>();
Calendar calendarToday = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String today = simpleDateFormat.format(calendarToday.getTime());
Calendar calendarDayBefore = Calendar.getInstance();
calendarDayBefore.setTime(calendarDayBefore.getTime());
int daysCounter = 0;
while (daysCounter <= 7) {
if (daysCounter == 0) { // means that its present day
listDate.add(today);
} else { // subtracts 1 day after each pass
calendarDayBefore.add(Calendar.DAY_OF_MONTH, -1);
Date dateMinusOneDay = calendarDayBefore.getTime();
String oneDayAgo = simpleDateFormat.format(dateMinusOneDay);
listDate.add(oneDayAgo);
}
daysCounter++;
}
return listDate;
}
}
此代码为我提供了正确的值,但它们没有按顺序排列,所以我在特定的日子里得到了错误的值。
所以我要做的就是同时执行7个调用,我用zip运算符猜测但是我没有为此提出解决方案,所以任何类型的帮助都会受到赞赏。
可以在此处找到API文档:http://hnbex.eu/api/v1/
答案 0 :(得分:0)
所以我要解决的问题是我在列表中添加了所有7个observable,然后我在该列表中调用了zipIterable()