1

I am using ical4j to parse the ical format on the Android.

My input ics is:

    BEGIN:VCALENDAR
    BEGIN:VEVENT
    SEQUENCE:5
    DTSTART;TZID=US/Pacific:20021028T140000
    DTSTAMP:20021028T011706Z
    SUMMARY:Coffee with Jason
    UID:EC9439B1-FF65-11D6-9973-003065F99D04
    DTEND;TZID=US/Pacific:20021028T150000
    END:VEVENT
    END:VCALENDAR

But when I try to parse this I get the exception:

Error at line 1: Expected [VCALENDAR], read [VCALENDARBEGIN]

The relevant code is:

    HttpHelper httpHelper = new HttpHelper(
            "http://10.0.2.2/getcalendar.php", params);
    StringBuilder response = httpHelper.postData();

    StringReader sin = new StringReader(response.toString());
    CalendarBuilder builder = new CalendarBuilder();
    Calendar cal = null;
    try {
        cal = builder.build(sin);
    } catch (IOException e) {
        Log.d("calendar", "io exception" + e.getLocalizedMessage());
    } catch (ParserException e) {
        Log.d("calendar", "parser exception" + e.getLocalizedMessage());

}

public class HttpHelper {
final HttpClient client;
final HttpPost post;
final List<NameValuePair> data;

public HttpHelper(String address, List<NameValuePair> data) {
    client = new DefaultHttpClient();
    post = new HttpPost(address);
    this.data = data;
}

private class GetResponseTask extends AsyncTask<Void, Void, StringBuilder> {
    protected StringBuilder doInBackground(Void... arg0) {
        try {
            HttpResponse response = client.execute(post);
            return inputStreamToString(response.getEntity().getContent());
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }
        return null;
    }
}

public StringBuilder postData() {
    try {
        post.setEntity(new UrlEncodedFormEntity(data));
        return (new GetResponseTask().execute()).get();
    } catch (UnsupportedEncodingException e) {
    } catch (InterruptedException e) {
    } catch (ExecutionException e) {
    }
    return null;
}

private StringBuilder inputStreamToString(InputStream is)
        throws IOException {
    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    // Read response until the end
    while ((line = rd.readLine()) != null) {
        total.append(line);
        Log.v("debug", "Line: " + line);
    }
    // Return full string
    return total;
}
}
Jonny
  • 97
  • 2
  • 6

2 Answers2

4

My guess would be the line endings in the response string are different from what ical4j expects.

The standard specifies you should use CRLF (aka '\r\n').

Arnout Engelen
  • 6,709
  • 1
  • 25
  • 36
  • Do you know what the line endings should be? It doesn't seem to be on the ical4j website. – Jonny Sep 04 '11 at 16:40
  • I'm fetching the response string from a webpage (when I view the source of that webpage I just see that ics input on separate lines), how would I add the CRLF in? Thanks for your help so far – Jonny Sep 04 '11 at 16:47
  • Let me phrase that better: How can I use php to output it in ics format, so that the php output can be parsed ok? – Jonny Sep 04 '11 at 17:23
  • Do you have an URL that we could look at to verify the line endings are indeed wrong? – Arnout Engelen Sep 04 '11 at 17:42
  • That file has CRLF line endings like it should. I pulled it through the code you quoted above and it was parsed just fine - even when I replaced the line endings with single CR or LF characters. Can you express the problem as a complete failing test? might be something in the ical4j.properties settings. Which version of ical4j are you using? – Arnout Engelen Sep 04 '11 at 18:00
  • The library version is ical4j-v1.0. I don't currently have an ical4j.properties file, should there be one? What do you mean by a complete failing test? – Jonny Sep 04 '11 at 18:04
  • You have pasted a piece of 'relevant code'. This code seems to work fine here. Can you paste more of it (for example, include a filled 'response' string) so I can reproduce your problem? – Arnout Engelen Sep 04 '11 at 18:17
  • I've posted the rest of the code. Might the problem be that in the inputStreamToString method readLine() returns the line without the line break characters? – Jonny Sep 04 '11 at 18:32
  • 1
    After testing, I can confirm the problem is with the readLine(). I need to manually add \r\n after each line – Jonny Sep 04 '11 at 19:37
3

The input file is valid, the problem is with your method inputStreamToString(), which performs a readLine() on the input (which strips the newlines) and appends to a string without re-adding the newlines.

I would suggest either to use an alternative to readLine() (if you want to preserve newlines from the original file), or append your own newlines in the loop, eg:

while ((line = rd.readLine()) != null) {
    total.append(line);
    total.append("\r\n");
    Log.v("debug", "Line: " + line);
}
fortuna
  • 701
  • 5
  • 7