Domanda

Sto usando ical4j per analizzare il formato iCal su Android.

I miei ci ingresso è:

    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

Ma quando provo ad analizzare questo ottengo l'eccezione:

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

Il codice relativo è:

    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;
}
}
È stato utile?

Soluzione

La mia ipotesi sarebbe la fine riga nella stringa di risposta sono diversi da quello che si aspetta ical4j.

previsto dalla norma si dovrebbe usare CRLF (aka '\ r \ n').

Altri suggerimenti

Il file di input è valido, il problema è con il metodo inputStreamToString (), che svolge una readLine () sull'ingresso (che spoglia i ritorni a capo) e aggiunge ad una stringa senza ri-aggiungere le nuove righe.

vorrei suggerire sia di utilizzare in alternativa al readLine () (se si desidera mantenere a capo dal file originale), o aggiungere i propri ritorni a capo nel circuito, ad esempio:

while ((line = rd.readLine()) != null) {
    total.append(line);
    total.append("\r\n");
    Log.v("debug", "Line: " + line);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top