1

Why is it not possible to loop a call execute to get multiple responses inside my IntentService via Retrofit?

Please see my code:

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;

    }
}

So the situation is that I have an API which has a url: http//:myapi.com/[date] which when called via retrofit gives me a JSON response of the Agenda(Events) for that specific day. What I want to do is to show the agenda(events) for the upcoming week, that is why I did it via loop given a String array of dates for the upcoming week. Imagine kind of like the Eventbrite app.

What I am doing wrong? I read somewhere that I should do this via JobQueue/Eventbus, should I be doing that? But I am a bit hesitant because I don't want to be using any more third party libraries. However, if that is the last case scenario I will probably just go with that then.

halfer
  • 19,824
  • 17
  • 99
  • 186
mangu23
  • 884
  • 9
  • 13

1 Answers1

0

Nevermind guys. That was because of a very stupid mistake i made.

I just changed:

List<Agenda> agendaList = null;

to

List<Agenda> agendaList = new ArrayList<>();
mangu23
  • 884
  • 9
  • 13