Android 访问用户时出现NullPointerException';使用谷歌日历API的日历列表
我想使用GoogleCalendarAPI来提取日历、事件等。我有android中AccountManager类的身份验证令牌。因此,通过这种方式,如果用户使用其google帐户登录手机,那么我可以轻松检索与他相关的身份验证令牌。 之后,我将执行以下操作:Android 访问用户时出现NullPointerException';使用谷歌日历API的日历列表,android,nullpointerexception,google-calendar-api,Android,Nullpointerexception,Google Calendar Api,我想使用GoogleCalendarAPI来提取日历、事件等。我有android中AccountManager类的身份验证令牌。因此,通过这种方式,如果用户使用其google帐户登录手机,那么我可以轻松检索与他相关的身份验证令牌。 之后,我将执行以下操作: public Calendar useCalendarAPI(final String accessToken) { try{ HttpTransport transport = new NetHttpTransport(
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”的日历对象
这是从Google日历API文档中复制的。
检查此链接但是我在下面一行得到了null指针异常
CalendarList calendarList = calendarService.calendarList().list().execute();
请提出一些建议
谢谢嘿,我有一个小的工作样本,它也是用你提到的同一个日历文档制作的。如果您可以随代码一起提供堆栈跟踪,也会有所帮助
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();
}
}
看起来您需要将
calendarService
作为参数传递给getCalendarsList()
函数我通过Google calendar private API实现了日历集成。谢谢
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();
}
}