Question

I am trying to parse an icalendar file (.ics) using ical4j library, and its working fine with all versions of Android but IceCreamSandwich and JellyBean.

Can someone tell me why its throwing FileNotFound Error only in ICS and JB but not in other versions of android?

Here's my code :

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

    }

}

Also, I have noticed that if I use Calendar.load(URL url) it works fine. So it is the saving and loading of file that is going wrong.

Was it helpful?

Solution

Try removing

c.setDoOutput(true);

(as suggested by this blog post)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top