为什么不可能通过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]
,当通过改造调用时,它会给我一个特定日期的议程(事件)的JSON响应。我想做的是显示即将到来的一周的议程(事件),这就是为什么我通过循环给出即将到来的一周的日期字符串数组。想象一下类似Eventbrite的应用程序。
我做错了什么?我在某个地方读到,我应该通过JobQueue/Eventbus做到这一点,我应该这样做吗?但是我有点犹豫,因为我不想再使用任何第三方库。但是,如果这是最后一种情况,那么我可能会使用它。
别介意,伙计们。那是因为我犯了一个非常愚蠢的错误。
I just changed:
List<Agenda> agendaList = null;
List<Agenda> agendaList = new ArrayList<>();