• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Downloading a File to Device

 
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So thanks to help yesterday, I can now parse the xml file I need.  Today I would like to be able to download this file through the app, so it can then be parsed.

I've got what looks to be the code I need.  It doesn't throw any errors at least and looks like this -


It is then called by this code -



The problem is, it does flag a runtime error as clearly I need to extend AsyncTask.  I've tried to do this, but can't seem to get the code right.  How should it look?

Thanks for the help!
 
Saloon Keeper
Posts: 7590
177
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Network access should always be done in the background on Android, so as not too block the main thread for an indeterminate amount of time. AsyncTask is one way to do this, but you can also start a thread something like this:


Or use Android's DownloadManager to handle it for you: https://www.sandersdenardi.com/using-the-android-downloadmanager/
 
Andy Crowther
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I feel like I'm getting there, but back to the file not found issues I had with parsing.  My code is now -


And called by -


But I get the error pointing at the FileOutputStream -



I understand I can't save to raw, hence the data/data/... saveDir.

Any ideas?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that directory exist?
The OutputStream won't create it.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I would use the tools available.
As Tim says, there's a DownloadManager built into Android.
 
Tim Moores
Saloon Keeper
Posts: 7590
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't try to construct absolute paths on an Android device. See https://developer.android.com/guide/topics/data/data-storage.html#filesInternal for how to read and write files.
 
Andy Crowther
Ranch Hand
Posts: 70
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, just posting to say thanks for the help.  I did some research on DownloadManager and currently I'm downloading the xml to the Downloads folder and parsing it from there.  Perhaps at somepoint I'll look into parsing it direct from the http source, but for now it works at least!
reply
    Bookmark Topic Watch Topic
  • New Topic