Is there any example of using robospice library for downloading large files? I've read there is BigBinaryRequest for it but what if there will be connectivity lost/ device reboot duing file download? After next execute call download will resume/restart or request success listener will fire with reference to damaged (not completly downloaded) file?
Maybe someone have experience of using robospice for such requests.
P.S. I know that there is native DownloadManager in Android, but I think using robospice is easier. Maybe I'm wrong.
#rciovati got it right, your download, if interrupted will be lost as RS won't be able to load the result from the cache. Or even worse, you could have receive an uncomplete InputStream from the cache. In that case you should remove the cache content by yourself (using the spicemanager's method to achieve it is pretty easy).
If you download twice a large input stream using the same cache key, there is no protection against that in RS. Your cache will get corrupted.
This answer may give you the feeling that BigBinaryRequest is poorly designed and not working, but according to my own experience, it works fine in all cases I met up to now.
Related
As our typical old asynctask network connection is getting replaced by volley. It's fast as I have used so I know. I downloaded images and showed. When I again restarted the app it didn't take time to download. So is it caching images for sometime ? And as it is recommended not to use in heavy download, any reason ? Or it is the reason that it keeps images and all in memory ? I saw I/O video of volley. But I need clear explanation. Thanks
I mostly use Volley for only GET, POST api calls because Volley holds all responses in memory during parsing. For large download operations, consider using an alternative like DownloadManager. Source: https://developer.android.com/training/volley/index.html
I have used Volley in one project and it was simple for normal POST,GET but I think that I had quite some problems when I tried to upload an image.
So I would say that is quite good for making just POST,GET where you don't transfer a lot of data.
I am making a download manager for my app. It works fine, but as soon as network fluctuates, I have to press retry button and it starts downloading from the beginning again. Any way to resume it from last break point?
Thanks,
Rahul
As the documentation (http://developer.android.com/training/volley/index.html) says
Volley is not suitable for large download or streaming operations, since Volley holds all responses in memory during parsing. For large download operations, consider using an alternative like DownloadManager.
However you could use the volley calls to download your big file in chunks, then when you have server instability you could just request the chunks you don't have. Note this is generally a bad idea as you're basically recreating tcp and you shouldn't do that.
create a temporary directory and store the fragment of the files that is being downloaded. after downloading concatenate the files.
I'm writing an app for both iOS and Android, and both are native (no PhoneGap, no Titanium). The app's general behavior is to GET an XML file from a server, parse it, and based on its contents GET some number of other XML files as well as to download some extra data files ( PDFs, images, etc ). It's a bit like RSS.
An initial load on iOS (when I've cleared local caches so everything has to be pulled down) takes about 20 seconds. Less than 1 second for the main feed, and 19 or 20 for the rest of the content.
On Android, it takes about two minutes.
Now, early on I found that XML parsing was a big hit so I dropped XmlPullParser and wrote a SAX parser which sped things up a bit (one XML file went from 20 seconds to parse down to 2, which is still really slow compared to iOS). But still, now that XML parsing is less of a factor in performance, the network performance is killing me.
I've googled and found a lot of threads on exactly this. I've tried a bunch of different approaches, none of which have done anything and it feels like guesswork more than engineering.
For example, I've done this:
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("http.keepAlive", "false");
And on my HttpURLConnection, I've set it to use NO_PROXY and a shorter connect timeout, and to disallow caches.
HttpURLConnection c = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
c.setConnectTimeout(1000);
c.setUseCaches(false);
None has helped!
Clearly, Android's network stack works and it works quickly - various Android apps I've used that touch the network are perfectly quick. So clearly I'm doing something wrong. But what?
Please, help. I'm at wit's end here.
I've not solved the problem, but I've worked out what's going on. It was latency.
Android's HttpURLConnection apparently has a broken http keep-alive implementation
Maybe related to the above problem, http pipelining doesn't work either
I was synchronously making URL connections in a single AsyncTask - I tried various approaches to speed it up like doing all my connections one after another, saving their response bodies in memory and processing that later. That sped it up from 2 or 3 minutes seconds to about 1.5, but that was still unacceptable.
In a last desperate hope I tried AsyncHttpClient
And holy smokes, now the whole download takes about 20 seconds, just like it does on iOS.
So, word to the wise: If your android app makes a LOT of GETs to a service, and you can't rewrite that service to elide those GETs into a single big batch ( I couldn't here ) you do not want to use Android's HttpUrlConnection. Use AsyncHttpClient.
Unfortunately, AsyncHttpClient is structured like AFNetworking, which is a lot more awkward on Java than it is in ObjectiveC. So it will take some finagling. But it works really well.
I need to download a big file on my app (almost 2gb) and i was wondering what is the best way to do that and some libs to help me.
First I looked the Android Asynchronous Http Library but I didn't find examples showing how to publish the progress or start, pause download. Then I don't know if should I use this lib or just use a standart http commons.
Other problem is, should I use a service to download my file??
Could you guys give a hint, how to do that?
I've see you got a bunch of answer and none of them really answers it from a holistic point of view.
To download a 2k or 2gb file the actual code is the same using some type of connection handler and some type of input stream and output stream and usually wrapping the input stream with a buffered input stream. For that you can find infinite amount of java examples all around the web.
The trick here considering the Android platform is that because it's a lengthy operation, you shouldn't be doing it inside the activity life cycle at all.
Said that you have two options:
as you suggested you can create a Service, I would suggest you to use the IntentService
as it seems to be a perfect fit for this case. The IntentService automatically spans a new thread, through the intent you pass the URL as a String and let the service download using the streams, etc.
another method that probably work well, but I've never personally used, is to use the DownloadManager that is available since Gingerbread. It shouldn't be difficult to call getSystemService(Context.DOWNLOAD_SERVICE) and call dwManag.enqueue(dwRequest);
hope it helps.
If you're not targeting pre-Gingerbread devices, I would use DownloadManager as suggested as the third option in the answer you linked to. It takes care of downloading the file, displays the progress in the notification bar so that the user can see what's going on even after your app has gone into the background and you don't have to worry so much about what happens when the user goes into another app and android decides to kill your app to free memory. Also, it works with features like "only download files over wifi" that at least some android builds have.
I suggest you to use adm download manager. Downloads never fail even if there is no network and the speed is also best.
I'm writing an application for Android which let users browse a list of files and download them.
For every download, I created a thread and I download the file with an HttpURLConnection instance (by reading from the connection in a while loop).
This method works fine with one active download. But when user starts more than one, download performance degrades dramatically. Most of the time, these parallel downloads consume all the bandwidth and the users is unable to browse files (which uses another HttpUrlConnection to load the files list).
Any suggestions on refining the download system?
Thanks.
P.S.: The method that popular browsers such as Google Chrome and Firefox do seems good. Anyone knows how they work?
Alas, i don't know of a way to throttle certain connections. However, a practical approach would be to implement a queue of downloads to control the number of simultaneous downloads. In your case, you would probably want to only let 1 thing download at a time. This can be implemented a few different ways.
Here's a way to do it with Handlers and a Looper: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/
Edit 1:
See mice's comment. It may be smarter to have a max of 2 threads downloading at a time.
You might want to check out the DownloadManager class in the android SDK.. Its only available above or equal api level 2.3 though.
http://developer.android.com/reference/android/app/DownloadManager.html
Some tutorials you might want to see..
http://jaxenter.com/downloading-files-in-android.1-35572.html
http://www.vogella.de/blog/2011/06/14/android-downloadmanager-example/