0

您好我想添加事件到谷歌api-java客戶端和calender API服務爲一個android應用程序的日曆。我使用Yaniv Inbar創建的calendersample項目作爲模板,效果很好。當插入1事件到選定的日曆工作完美,但當我嘗試批量添加事件到日曆獲取非法狀態異常。批量添加事件到谷歌日曆

在這個例子中,你可以像這樣批量添加日曆。 全班都可以在這裏AsyncBatchInsertCalendars.java

@Override 
protected void doInBackground() throws IOException { 
    BatchRequest batch = client.batch(); 
    for (Calendar calendar : calendars) { 
     client.calendars().insert(calendar).setFields(CalendarInfo.FIELDS) 
     .queue(batch, new JsonBatchCallback<Calendar>() { 
     public void onSuccess(Calendar calendar, GoogleHeaders headers) { 
      model.add(calendar); 
     } 
     @Override 
     public void onFailure(GoogleJsonError err, GoogleHeaders headers) 
     throws IOException { 
      Utils.logAndShowError(activity, CalendarSampleActivity.TAG, err.getMessage()); 
     } 
     }); 
    } 
    batch.execute(); 
} 

發現我重寫了類,所以,這將是事件,而不是壓光機。如果你看看整個班級AsyncBatchInsertEvent.java,你會發現在doInBackground方法中,我還通過一個數組列表循環來創建事件列表。應該將其添加到要在給定日曆上插入的批次。

@Override 
protected void doInBackground() throws IOException { 
    BatchRequest batch = client.batch(); 
    for (Event event : events) { 
     client.events().insert(calender.id, event).queue(batch, 
     new JsonBatchCallback<Event>() { 
     public void onSuccess(Event event, GoogleHeaders headers) { 
      //TODO show succes message. 
     } 
     @Override 
     public void onFailure(GoogleJsonError err, GoogleHeaders headers) 
     throws IOException { 
      Utils.logAndShowError(activity, EventActivity.TAG, err.getMessage()); 
     } 
     }); 
    } 
    batch.execute(); 
} 

如果我用這個然後得到一個異常,應用程序崩潰

W/dalvikvm(21030): threadid=20: thread exiting with 
uncaught exception (group=0x40c19930) 
E/AndroidRuntime(21030): FATAL EXCEPTION: AsyncTask #2 
E/AndroidRuntime(21030): java.lang.RuntimeException: An error occured 
while executing doInBackground() 

錯誤的完整堆棧跟蹤可以在這裏找到在引擎收錄log.txt。有誰知道如何解決這個問題還是我錯誤地實現了代碼?整個代碼可以在這裏找到pastbin AsyncBatchInsertEvent.java

+0

我研究了這一些,似乎batch.execute();錯誤原因批次包含0個排隊項目。當我運行batch.size()時,輸出爲0 – Aegis

回答

1

愚蠢的我arraylist事件是空的,因爲我檢查了一個字符串==字符串,而不是string.equels(字符串)。