1

I want to make an android app that is actually a RSS reader. This will load XML file from a particular link like http://kalaerkantho.com/rss.xml. After downloading I know how to parse it. But my question is how to download it first so that I can process the downloaded file.

Enam Ahmed Shahaz
  • 195
  • 1
  • 2
  • 10

1 Answers1

0

Try this:

private static void downloadFile(String url, String filePath) {
    try {
        File outputFile = new File(filePath);
        URL u = new URL(url);
        URLConnection conn = u.openConnection();
        int contentLength = conn.getContentLength();

        DataInputStream stream = new DataInputStream(u.openStream());

        byte[] buffer = new byte[contentLength];
        stream.readFully(buffer);
        stream.close();

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
        fos.write(buffer);
        fos.flush();
        fos.close();
    } catch(FileNotFoundException e) {
        return; // swallow a 404
    } catch (IOException e) {
      return; // swallow a 404
    }
}

Adapted from this answer.

Community
  • 1
  • 1
sampathsris
  • 21,564
  • 12
  • 71
  • 98