为什么无法通过 Retrofit 循环调用执行以在我的 IntentService 中获得多个响应?
请看我的代码:
public class UpdateAgendaService extends IntentService {
public static final int STATUS_RUNNING = 0;
public static final int STATUS_FINISHED = 1;
public static final int STATUS_ERROR = 2;
private Agenda agenda;
public UpdateAgendaService() {
super(UpdateAgendaService.class.getName());
}
@Override
protected void onHandleIntent(Intent intent) {
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
String[] dateWeek = intent.getStringArrayExtra("dateWeek");
if (dateWeek != null) {
receiver.send(STATUS_RUNNING, Bundle.EMPTY);
Bundle bundle = new Bundle();
try {
//Why is this not possible?
List<Agenda> agendaList = getAgendaList(dateWeek);
receiver.send(STATUS_FINISHED, bundle);
}
} catch (Exception e) {
/* Sending error message back to activity */
bundle.putString(Intent.EXTRA_TEXT, e.toString());
receiver.send(STATUS_ERROR, bundle);
}
}
Log.d(Utilities.TAG, "Service Stopping!");
this.stopSelf();
}
private List<Agenda> getAgendaList(String[] upcomingWeekdates){
List<Agenda> agendaList = null;
for (int i = 0; i < upcomingWeekdates.length; i++) {
String weekDay = upcomingWeekdates[i];
agendaList.add(getAgenda(weekDay));
}
return agendaList;
}
private Agenda getAgenda(String date) {
Agenda agenda = null;
ApiService apiService = new QardioApi().getApiService();
Call<Agenda> call = apiService.getAgenda(date);
try {
agenda = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
return agenda;
}
}
所以情况是我有一个 API,它有一个 url:http//:myapi.com/[date]
当通过改造调用它时,它会给我一个特定日期的 Agenda(Events) 的 JSON 响应。我想要做的是显示下一周的议程(事件),这就是为什么我通过循环给出下一周的日期字符串数组来做到这一点。想象一下有点像 Eventbrite 应用程序。
我做错了什么?我在某处读到我应该通过 JobQueue/Eventbus 执行此操作,我应该这样做吗?但我有点犹豫,因为我不想再使用任何第三方库。但是,如果这是最后一种情况,那么我可能会继续这样做。