使用Google Calendar API访问用户的日历列表时出现NullPointerException

时间:2012-01-12 14:54:46

标签: android nullpointerexception google-calendar-api

我想使用Google Calendar Api来提取日历,事件等。我在android中的AccountManager类中拥有身份验证令牌。因此,如果用户使用他的Google帐户登录移动设备,那么我可以轻松检索与他相关的身份验证令牌。 之后,请执行以下操作:

 public Calendar useCalendarAPI(final String accessToken) {

 try{

      HttpTransport transport = new NetHttpTransport();
      AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken);
      Calendar.Builder builder = Calendar.builder(transport, new JacksonFactory());
      builder.setApplicationName("My App Name");
      builder.setHttpRequestInitializer(accessProtectedResource);
      JsonHttpRequestInitializer json = new JsonHttpRequestInitializer() {
          public void initialize(JsonHttpRequest request) {
                CalendarRequest calRequest = (CalendarRequest) request;
                calRequest.setKey(Common.API_KEY);
                calRequest.setOauthToken(accessToken);
            }
            };
      builder.setJsonHttpRequestInitializer(json);

      Calendar calendarService= builder.build();

      if(calendarService!= null)
      {
          return calendarService;
      }
      else
          return null;
      }catch (Exception e) {
        // TODO: handle exception
          e.getMessage();
    }

在此之后,我收到一个名为“calendarService”的Calendar对象。

public void getCalendarsList()
{
      try     
      {
            CalendarList calendarList = calendarService.calendarList().list().execute();

            while (true) {
                for (CalendarListEntry calendarListEntry : calendarList.getItems()) {
                    System.out.println(calendarListEntry.getSummary());
                }
                String pageToken = calendarList.getNextPageToken();
                if (pageToken != null && !pageToken.equalsIgnoreCase("")) {
                    calendarList = calendarService.calendarList().list().setPageToken(pageToken).execute();

                } else {
                    break;
                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}   

这是从Google Calendar API文档中复制的。 请查看此链接here
但我在下一行获得NullPointer异常

CalendarList calendarList = calendarService.calendarList().list().execute();

请提出建议。

由于

2 个答案:

答案 0 :(得分:0)

嘿,我有这个小工作样本,它是使用您提到的相同日历文档制作的。如果您可以将堆栈跟踪与代码一起提供,也会有所帮助。

 public static void main(String[] args) throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    String clientId = "CLIENT ID";
    String clientSecret = "CLIENT SECRET";
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
    String scope = "https://www.googleapis.com/auth/calendar";
    String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope)
    .build();
    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is the authorization code?");
    String code = in.readLine();
    AccessTokenResponse response = new GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
        clientId, clientSecret, code, redirectUrl).execute();
    GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
        response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
        response.refreshToken);

    Calendar calendarService = Calendar.builder(httpTransport, jsonFactory)
        .setApplicationName("YOUR_APPLICATION_NAME")
        .setHttpRequestInitializer(accessProtectedResource)
        .build();
    try     
    {
      com.google.api.services.calendar.model.CalendarList calendarList = calendarService.calendarList().list().execute();

      while (true) {
        for (CalendarListEntry calendarListEntry : calendarList.getItems()) {
          System.out.println(calendarListEntry.getSummary());
        }
        String pageToken = calendarList.getNextPageToken();
        if (pageToken != null && !pageToken.equalsIgnoreCase("")) {
          calendarList = calendarService.calendarList().list().setPageToken(pageToken).execute();

        } else {
          break;
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

答案 1 :(得分:0)

您似乎需要将calendarService作为参数传递给getCalendarsList()功能