Parsing ics file in Android takes too long

Help
Anonymous
2012-07-08
2013-06-05
  • Anonymous - 2012-07-08

    I am trying to Parse a icalendar file in Android using ical4j, So I download the file save it in internal storage and the retreieve and parse it. However

    calendar = builder.build(fis);
    

    is taking over 40 seconds . Is it normal for a the parser to take 40 seconds to build calendar instance from a  file that is around 150KB in size?

    Here's the full code:

    [code]@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);
    
            }
    
        }
        
    [/code]
    
     
  • Ben Fortuna

    Ben Fortuna - 2012-07-09

    Hi,

    40 seconds does seem like a long time for parsing, are you seeing this in the emulator or on an actual device? I think the eclipse tools support debugging, so if you are able to run it through the debugger you might be able to isolate where the bulk of the time is taken.

    If you are running on a device, perhaps rather than saving to local storage load the file into a byte array and use a ByteArrayInputStream rather than a FileInputStream. This would eliminate the possibility that IO is the source of the delay (not really a long term solution due to memory constraints, but just to help identify the issue).

    regards,
    ben

     
  • Anonymous - 2012-07-09

    Thanks Ben,

    I tried straight downloading and displaying the ical file (without storing it) but its still same problem. Also I tried Calendar.load method but still the same problem, However, when I use the same code and run it in  Java in PC, it works fine and parses the calendar in under a second.  Sorry I couldn't use ByteArrayInputStram as it was throwing a ClassCastException.

    Also, it takes long in both the emulator and the real phone.

    Also, I copied the logcat, there seems a lot of garbage collection and heap seems to be quite full, probably  a memory leak somewhere,not sure, Any Idea?

    07-09 18:26:08.917: I/dalvikvm(952): threadid=3: reacting to signal 3
    07-09 18:26:09.177: E/dalvikvm(952): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
    07-09 18:26:09.427: I/dalvikvm(952): threadid=3: reacting to signal 3
    07-09 18:26:09.558: I/dalvikvm(952): Wrote stack traces to '/data/anr/traces.txt'
    07-09 18:26:09.947: I/dalvikvm(952): threadid=3: reacting to signal 3
    07-09 18:26:10.007: I/dalvikvm(952): Wrote stack traces to '/data/anr/traces.txt'
    07-09 18:26:10.267: D/gralloc_goldfish(952): Emulator without GPU emulation detected.
    07-09 18:26:11.828: D/dalvikvm(952): GC_CONCURRENT freed 193K, 4% free 9284K/9607K, paused 34ms+9ms
    07-09 18:26:13.327: D/dalvikvm(952): GC_CONCURRENT freed 293K, 5% free 9455K/9863K, paused 11ms+10ms
    07-09 18:26:13.897: D/dalvikvm(952): GC_CONCURRENT freed 260K, 4% free 9602K/9991K, paused 21ms+10ms
    07-09 18:26:14.467: D/dalvikvm(952): GC_CONCURRENT freed 179K, 3% free 9829K/10119K, paused 12ms+11ms
    07-09 18:26:15.217: D/dalvikvm(952): GC_CONCURRENT freed 285K, 4% free 9961K/10375K, paused 12ms+11ms
    07-09 18:26:16.707: D/dalvikvm(952): GC_CONCURRENT freed 334K, 5% free 10044K/10503K, paused 12ms+13ms
    07-09 18:26:17.907: D/dalvikvm(952): GC_CONCURRENT freed 371K, 5% free 10071K/10567K, paused 12ms+13ms
    07-09 18:26:19.066: D/dalvikvm(952): GC_CONCURRENT freed 403K, 6% free 10098K/10631K, paused 12ms+7ms
    07-09 18:26:20.376: D/dalvikvm(952): GC_CONCURRENT freed 381K, 5% free 10120K/10631K, paused 20ms+14ms
    07-09 18:26:21.556: D/dalvikvm(952): GC_CONCURRENT freed 413K, 6% free 10152K/10695K, paused 12ms+6ms
    07-09 18:26:22.716: D/dalvikvm(952): GC_CONCURRENT freed 391K, 5% free 10175K/10695K, paused 12ms+14ms
    07-09 18:26:23.856: D/dalvikvm(952): GC_CONCURRENT freed 368K, 5% free 10199K/10695K, paused 11ms+13ms
    07-09 18:26:25.076: D/dalvikvm(952): GC_CONCURRENT freed 403K, 5% free 10225K/10759K, paused 12ms+19ms
    07-09 18:26:26.217: D/dalvikvm(952): GC_CONCURRENT freed 378K, 5% free 10249K/10759K, paused 12ms+15ms
    07-09 18:26:27.417: D/dalvikvm(952): GC_CONCURRENT freed 415K, 6% free 10278K/10823K, paused 11ms+15ms
    07-09 18:26:28.487: D/dalvikvm(952): GC_CONCURRENT freed 392K, 5% free 10302K/10823K, paused 11ms+6ms
    07-09 18:26:29.607: D/dalvikvm(952): GC_CONCURRENT freed 370K, 5% free 10324K/10823K, paused 6ms+14ms
    07-09 18:26:30.867: D/dalvikvm(952): GC_CONCURRENT freed 402K, 5% free 10356K/10887K, paused 11ms+13ms
    07-09 18:26:31.987: D/dalvikvm(952): GC_CONCURRENT freed 380K, 5% free 10376K/10887K, paused 11ms+21ms
    07-09 18:26:33.207: D/dalvikvm(952): GC_CONCURRENT freed 412K, 5% free 10411K/10951K, paused 19ms+14ms
    07-09 18:26:34.297: D/dalvikvm(952): GC_CONCURRENT freed 389K, 5% free 10431K/10951K, paused 14ms+8ms
    07-09 18:26:35.417: D/dalvikvm(952): GC_CONCURRENT freed 367K, 5% free 10457K/10951K, paused 11ms+13ms
    07-09 18:26:36.627: D/dalvikvm(952): GC_CONCURRENT freed 404K, 5% free 10484K/11015K, paused 11ms+14ms
    07-09 18:26:37.747: D/dalvikvm(952): GC_CONCURRENT freed 376K, 5% free 10508K/11015K, paused 11ms+14ms
    07-09 18:26:38.877: D/dalvikvm(952): GC_CONCURRENT freed 412K, 5% free 10538K/11079K, paused 11ms+8ms
    07-09 18:26:40.207: D/dalvikvm(952): GC_CONCURRENT freed 390K, 5% free 10560K/11079K, paused 12ms+43ms
    07-09 18:26:41.337: D/dalvikvm(952): GC_CONCURRENT freed 368K, 5% free 10583K/11079K, paused 12ms+14ms
    07-09 18:26:42.417: D/dalvikvm(952): GC_CONCURRENT freed 402K, 5% free 10612K/11143K, paused 12ms+8ms
    07-09 18:26:43.647: D/dalvikvm(952): GC_CONCURRENT freed 434K, 6% free 10644K/11207K, paused 12ms+14ms
    07-09 18:26:44.857: D/dalvikvm(952): GC_CONCURRENT freed 408K, 5% free 10672K/11207K, paused 12ms+14ms
    07-09 18:26:46.167: D/dalvikvm(952): GC_CONCURRENT freed 436K, 6% free 10704K/11271K, paused 12ms+15ms
    07-09 18:26:47.436: D/dalvikvm(952): GC_CONCURRENT freed 408K, 5% free 10732K/11271K, paused 12ms+15ms
    07-09 18:26:48.756: D/dalvikvm(952): GC_CONCURRENT freed 439K, 6% free 10767K/11335K, paused 12ms+15ms
    07-09 18:26:50.106: D/dalvikvm(952): GC_CONCURRENT freed 465K, 6% free 10802K/11399K, paused 14ms+7ms
    07-09 18:26:51.816: D/dalvikvm(952): GC_CONCURRENT freed 494K, 6% free 10841K/11463K, paused 22ms+9ms
    07-09 18:26:53.117: D/dalvikvm(952): GC_CONCURRENT freed 463K, 6% free 10870K/11463K, paused 12ms+7ms
    07-09 18:26:54.577: D/dalvikvm(952): GC_CONCURRENT freed 489K, 6% free 10909K/11527K, paused 56ms+15ms
    07-09 18:26:56.077: D/dalvikvm(952): GC_CONCURRENT freed 511K, 6% free 10950K/11591K, paused 11ms+17ms
    07-09 18:26:57.558: D/dalvikvm(952): GC_CONCURRENT freed 534K, 6% free 10995K/11655K, paused 12ms+9ms
    07-09 18:26:59.167: D/dalvikvm(952): GC_CONCURRENT freed 558K, 6% free 11032K/11719K, paused 11ms+16ms
    
     
  • Anonymous - 2012-07-10

    I have tried different ics file links including the one in the sample projects and they all take range of times to build some taking as little as a second to build (like the one in the sample) and some take as long as 40 seconds ( like mine) . I would be glad if you have a look at my ics file and decide if 40 seconds is reasonable?

    https://www.google.com/calendar/ical/m0es4hhj4g9d69ibak88tvoup0%40group.calendar.google.com/public/basic.ics

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks