0

I'm rather new to all this, so please forgive me. I'm trying to download an XML file to the device, which can then be parsed. The code I have at the moment is -

public class DownloadFromURL implements Runnable {
    private static final int BUFFER_SIZE = 4096;

    @Override
    public void run() {
        Log.i("Download From URL", "Run");

        String fileURL = "http://m.highways.gov.uk/feeds/rss/AllEvents.xml";
        String saveDir = "/data/data/com.androidandyuk/files";
        URL url;
        try {
            url = new URL(fileURL);

            HttpURLConnection httpConn = (HttpURLConnection) 
            url.openConnection();

            int responseCode = httpConn.getResponseCode();
            Log.i("Download from URL", "Resp Code :" + responseCode);
            // always check HTTP response code first
            if (responseCode == HttpURLConnection.HTTP_OK) {
                String fileName = "";
                String disposition = httpConn.getHeaderField("Content-Disposition");
                String contentType = httpConn.getContentType();
                int contentLength = httpConn.getContentLength();

                if (disposition != null) {
                    // extracts file name from header field
                    int index = disposition.indexOf("filename=");
                    if (index > 0) {
                        fileName = disposition.substring(index + 10,
                                disposition.length() - 1);
                    }
                } else {
                    // extracts file name from URL
                    fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                            fileURL.length());
                }

                // opens input stream from the HTTP connection
                InputStream inputStream = httpConn.getInputStream();
                String saveFilePath = saveDir + File.separator + fileName;
                Log.i("Download from URL", "Opening a Stream");
                // opens an output stream to save into file
                FileOutputStream outputStream = new 
                FileOutputStream(saveFilePath);

                int bytesRead = -1;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                outputStream.close();
                inputStream.close();

                Log.i("Download", "File downloaded");
            } else {
                Log.i("Download", "No file to download. Server replied HTTP code: " + responseCode);
            }

            httpConn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Which is then run by -

DownloadFromURL getTraffic = new DownloadFromURL();
            Thread t = new Thread(getTraffic);
            t.start();

But as it runs, I get an error saying 'java.io.FileNotFoundException: /data/data/com.androidandyuk/files/AllEvents.xml (No such file or directory)'

Can anyone please help?

(And if you want to downvote me, please at least comment why, as I get downvoted even when I feel I've formatted my question well... and done lots of searching for the answer already! Thanks)

AndyCr15
  • 465
  • 5
  • 17
  • refer http://stackoverflow.com/questions/8986376/how-to-download-xml-file-from-server-and-save-it-in-sd-card – sasikumar May 18 '17 at 11:08
  • try to use `saveDir.mkdirs();` and then `new File(saveFilePath).createFile()` and then write it. – SRB Bans May 18 '17 at 11:09
  • Thanks. Should that read 'use' saveDir.dkdirs();? I get cannot resolve method dkdirs() error in Android Studio? – AndyCr15 May 18 '17 at 11:15
  • @sasikumar Using that code gives me the same error. 'No such file or directory' – AndyCr15 May 18 '17 at 11:19
  • @AndyCr15 updated my comment.. its `mkdirs()` to make directories if not exists,, then create a file and write the input stream there. – SRB Bans May 18 '17 at 11:19
  • @SRBbans thanks, but I get the same message about not resolving the method? saveDir is a String. From what I see about mkdirs() I use it on a file? – AndyCr15 May 18 '17 at 11:22
  • Thats because `saveDir` is a `String` and not a `File`. But do you really want to hard code the path to the files directory? Why not use `Context.getDir()`? – Henry May 18 '17 at 11:27
  • getFilesDir() you mean. – greenapps May 18 '17 at 12:17

0 Answers0