Aegis
Aegis

Reputation: 5801

batch adding events to google calender

Hi I'm trying to add events to a calender with the google-api-java-client and calender API service for a android app. I used the calendersample project created by Yaniv Inbar as a template which works great. When inserting 1 event to the selected calender works perfectly but when i try to batch add events to the calendar get an Illegal state exception.

in the example you could batch add calenders like this. whole class can be found here 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();
}

I rewrote the class so that it would be events instead of calenders. if you look at the whole class AsyncBatchInsertEvent.java you'll see that in the doInBackground methode I also loop through a arraylist creating a list of events. which should than be added to the batch to be inserted on the given calendar.

@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();
}

If I use this then get a an exception and the app crashes

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()

The full stacktrace of the error can be found here at pastebin log.txt. Does anyone know how to fix this or did I implement the code incorrectly? the whole code can be found here at pastbin AsyncBatchInsertEvent.java

Upvotes: 0

Views: 1106

Answers (1)

Aegis
Aegis

Reputation: 5801

Stupid me, the arraylist events was empty cause i checked a string == string instead of string.equels(string).

Upvotes: 1

Related Questions