首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析格式-解析异常

解析格式-解析异常
EN

Stack Overflow用户
提问于 2011-09-05 00:12:21
回答 2查看 2.9K关注 0票数 1

我正在使用ical4j来解析安卓系统上的ical格式。

我的输入ics是:

代码语言:javascript
复制
    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

但是当我尝试解析它时,我得到了一个异常:

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

相关代码为:

代码语言:javascript
复制
    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());

}

代码语言:javascript
复制
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;
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-05 00:37:17

我的猜测是响应字符串中的行尾与ical4j期望的不同。

该标准指定您应该使用CRLF (也称为'\r\n')。

票数 4
EN

Stack Overflow用户

发布于 2011-09-05 07:51:52

输入文件是有效的,问题出在您的方法inputStreamToString()上,该方法对输入执行readLine() (去掉换行符)并附加到字符串,而不重新添加换行符。

我建议使用readLine()的替代方法(如果您想保留原始文件中的换行符),或者在循环中添加您自己的换行符,例如:

代码语言:javascript
复制
while ((line = rd.readLine()) != null) {
    total.append(line);
    total.append("\r\n");
    Log.v("debug", "Line: " + line);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7300494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档