我对编程还很陌生,对于大多数人来说都是自学成才的,所以请耐心等待。因此,我正在与几个朋友一起开发一个应用程序,作为一个学校项目,实际上进展顺利,但是我有一个问题,因为对我来说这没有逻辑性,我似乎无法解决问题。
好的,我想从API调用Google日历以加载到应用程序中,当从同一类中的main方法调用时,一切都很好。该方法返回一个包含不同事件的HashMap。
public static HashMap nextEvent() throws IOException, ParseException {
String place;
String startTime;
String title;
String day;
HashMap<Integer, HashMap> info = new HashMap<>();
//Creates calendar
// final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
final NetHttpTransport HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport();
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) ---> (line 143)
.setApplicationName("Hi")
.build();
DateTime now = new DateTime(System.currentTimeMillis());
Events events = service.events().list(calendarId)
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
if (items.isEmpty()) {
return null;
} else {
int i = 0;
for (Event event : items) {
HashMap<String, String> ev = new HashMap<>();
//Converts date format from DateTime to hh:mm
SimpleDateFormat formatter, FORMATTER, FORMATTERDate;
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
String oldDate = getTime().toString();
Date date = formatter.parse(oldDate.substring(0, 26) + oldDate.substring(27, 29));
FORMATTER = new SimpleDateFormat("hh:mm a");
//Finds the date
FORMATTERDate = new SimpleDateFormat("dd/MM");
startTime = (FORMATTER.format(date));
day = (FORMATTERDate.format(date));
place = event.getLocation();
title = event.getSummary();
ev.put("Place", place);
ev.put("Title", title);
ev.put("Time", startTime);
ev.put("Day", day);
info.put(i, ev);
i++;
}
return info;
}
}
public static void main(String[] args) throws Exception{
HashMap h = nextEvent();
for (int i = 0; i < h.size(); i++) {
System.out.println(h.get(i));
}
}
它输出
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 365}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 616}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 276}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 359}
{Title=..., Time=10:30 AM, Day=16/01, Place=Rooms 327/329}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 332a}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 359}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 276}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 366}
{Title=..., Time=10:30 AM, Day=16/01, Place=Room 618}
这正常工作,但是当我尝试从MainActivity类调用该方法以设置包含内容的文本字段时,出现IO异常。只是对我来说没有道理
当我运行这个
public void setEvent1() throws ParseException, GeneralSecurityException, IOException {
TextView event1 = findViewById(R.id.event1name);
TextView event1time = findViewById(R.id.event1time);
TextView event1place = findViewById(R.id.event1place);
HashMap<Integer, HashMap> h = nextEvent();
event1.setText(h.get(0).get("Title").toString());
event1time.setText(h.get(0).get("Time").toString());
event1place.setText(h.get(0).get("Place").toString());
}
我收到此错误
W/System.err: java.io.IOException: unable to create directory: /tokens
W/System.err: at com.google.api.client.util.store.FileDataStoreFactory.<init>(FileDataStoreFactory.java:61)
at com.fisapp.fisapp.CalendarQuickstart.getCredentials(CalendarQuickstart.java:55)
at com.fisapp.fisapp.CalendarQuickstart.nextEvent(CalendarQuickstart.java:143)
at com.fisapp.fisapp.MainActivity.setEvent1(MainActivity.java:64)
at com.fisapp.fisapp.MainActivity.onResume(MainActivity.java:52)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1412)
at android.app.Activity.performResume(Activity.java:7292)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3776)
W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3816)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
(这是getCredentials方法)
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = CalendarQuickstart.class.getResourceAsStream(CREDENTIALS_JSON);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,new InputStreamReader(in));
File file = new File("fisApp/../tokens");
FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(file); ----> (This is line 55)
DataStore<StoredCredential> dataStore = dataStoreFactory.getDataStore("StoredCredential");
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT,JSON_FACTORY, clientSecrets, SCOPES)
.setCredentialDataStore(dataStore).build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
我已经尝试了所有可以想到的方法,但是我无法解决这个问题。任何帮助将挽救生命!
预先感谢一堆!