2

我正在尝试使用 ical4j 库解析一个 icalendar 文件 (.ics),它可以在所有版本的 Android 上正常工作,但 IceCreamSandwich 和 JellyBean 除外。

有人能告诉我为什么它只在 ICS 和 JB 中抛出 FileNotFound 错误,而不是在其他版本的 android 中吗?

这是我的代码:

public class MainActivity extends Activity {
String foo = null;
TextView TextView = null;

String fileName = "ical.ics";
String URL = "https://www.google.com/calendar/ical/m0es4hhj4g9d69ibak88tvoup0%40group.calendar.google.com/public/basic.ics";
StringBuilder b = new StringBuilder();

@Override
public void onCreate(Bundle savedInstanceState) {

    if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
        }

    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.setDoOutput(true);
                    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> {



    @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();



        try {
            Calendar calendar = builder.build(fis);
            b.append(calendar.getProperty("X-WR-CALNAME").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());

                }
            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return null;
    }

    @Override
    protected void onPostExecute(Void Result) {


        TextView.setText(b.toString());

    }

}

另外,我注意到如果我使用 Calendar.load(URL url) 它工作正常。所以是文件的保存和加载出了问题。

4

1 回答 1

3

尝试删除

c.setDoOutput(true);

(正如这篇博文所建议的)

于 2012-07-08T17:01:51.487 回答