1

为什么不能循环调用execute来通过Retrofit在我的IntentService中获取多个响应?为什么无法循环调用execute来通过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; 

    } 
} 

所以的情况是,我有一个包含URL的API:http//:myapi.com/[date],当通过改造叫给我的议程(事件)的JSON响应该特定天。我想要做的是显示即将到来的一周的议程(事件),这就是为什么我通过循环给出了即将到来的一周的日期字符串数组。想象一下有点像Eventbrite应用程序。

我做错了什么?我在某处读过我应该通过JobQueue/Eventbus做到这一点,我应该这样做吗?但我有点犹豫,因为我不想再使用任何第三方库。但是,如果这是最后一种情况,那么我可能会随之而去。

回答

0

没关系的家伙。那是因为我犯了一个非常愚蠢的错误。

我只是改变:

List<Agenda> agendaList = null; 

List<Agenda> agendaList = new ArrayList<>(); 
相关问题