我正在尝试在 android 中使用 ical4j 解析谷歌日历 ical (.ics) 文件。但是从输入流构建日历需要 40 多秒。
calendar = builder.build(fis);
ical 文件大小只有 150KB。此外,当我使用相同的代码并在 PC 中运行它时,日历的构建会在不到一秒的时间内完成。我还注意到 LogCat 中有大量垃圾收集。谁能帮我?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView = (TextView)findViewById(R.id.Hello_World);
new Download().execute();
}
final class Download extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute(){
TextView.setText("Downloading");
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL(URL);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
FileOutputStream fos = openFileOutput(fileName, MainActivity.MODE_PRIVATE);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}
return null;
}
@Override
protected void onPostExecute(Void Result) {
TextView.setText("Saved...Loading Data");
new Loadicaldata().execute();
}
}
final class Loadicaldata extends AsyncTask<Void, Void, Void> {
String Summary = null;
@Override
protected Void doInBackground(Void... arg0) {
FileInputStream fis = null;
try {
fis = openFileInput(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_UNFOLDING, true);
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION, true);
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = null;
try {
calendar = builder.build(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
b.append(calendar.getProperty("X-WR-CALNAME").getValue());
ComponentList events = calendar.getComponents(Component.VEVENT);
VEvent Event = (VEvent) events.get(0);
Summary = Event.getDescription().getValue();
/*
for (Object event : calendar.getComponents(Component.VEVENT)) {
if (((VEvent) event).getSummary() != null) {
b.append("\n\n");
b.append(((VEvent) event).getSummary().getValue());
b.append(": ");
b.append(((VEvent) event).getStartDate().getDate());
}
}
*/
return null;
}
@Override
protected void onPostExecute(Void Result) {
TextView.setText(Summary);
}
}
LogCat: http ://dl.dropbox.com/u/35866688/LogCat.txt
此外,我可以安全地排除 IO 错误的可能性,因为 Calendar.load 方法也需要很长时间。
如果有人感兴趣,这是 ical 文件。 https://www.google.com/calendar/ical/m0es4hhj4g9d69ibak88tvoup0%40group.calendar.google.com/public/basic.ics