Resume failed download using retrofit2 - android

While downloading large files with Retrofit our downloads often fail due to poor mobile connection.
Is there any way to resume a failed download offered by retrofit?
Current Idea:
With the #Streaming annotation downloaded content should be directly downloaded to a file. If the download fails we could check the partly downloaded file for its length and then request the missing data via Content-Range http parameter.
Has anybody done this before? Is this possible?

Related

Android DownloadManager with gRPC

Currently working on an API which make the client (Android/Kotlin) being able to download a video from the server. With a traditional HTTP URI it would be a pretty easy task, I would just DownloadManager and the problem is solved. However, I'm using gRPC (c++) for the server.
I currently have a solution with Kotlin flows which basically take the unidirectional stream from the server and consume the file chunk to store them in local file. However, even if it works for the major parts, It has a lot of corner cases and that is why I want to use DownloadManager. For further information on what I have currently please check: Handling file download with gRPC on Android
After looking for a solution for a little while now, I found that it is possible to map the RPC call to REST API by using this project and basically do something like:
rpc Download(DownloadRequest) returns (DownloadResponse) {
option (google.api.http) = {
post: "/v1/download"
body: "*"
};
}
or use a YAML file to configure the google API.
All of this seems to be pretty convenient while using Google Cloud and Golang but I'm creating a standalone server in C++.
So there are two questions here:
Is it possible to serve static file in a REST way with gRPC c++
Is there another way to use DownloadManager with gRPC ?
As the commenter on the question suggested, gRPC is for structured data not raw file downloads. Trying to map between a URI and the RPC call would not be enough to make DownloadManager work with gRPC, because the gRPC server is going to send and expect additional data beyond the raw file itself that will not be understood or sent by DownloadManager.
You can certainly use gRPC server and client to send large files, but it's not going to have the same set of features built-in for large file downloading (such as resuming your download later if the connection is broken) as using something like a HTTP server + a specific file download library/client. I would recommend just running a separate HTTP server for your file download needs.
[crossposting my answer from the grpcio mailing list]

Load file on poor connection without download manager or third-party libraries

I wrote some library and it should load 3-5Mb files sometimes.
I tried to use HttpURLConnection but I can't restore download with it if connection was lost from the last place.
Also I tried DownloadManager, but it requires DOWNLOAD_WITHOUT_NOTIFICATION, which is unacceptable. Without it, it show load icon it top right corner, which is unacceptable also.
I can't use third-party libraries also.
Are there any code sample how to force to work HttpURLConnection on poor connection (EDGE, overloaded servers) or some other open source library which code I can integrate?
Have a look at Volley. It allows custom retry policies for a request. You can instantiate a customized DefaultRetryPolicy and set it to your request using setRetryPolicy.
Be careful, because Volley caches the whole downloaded chunk in memory.
There might be other libraries out there, so check them out before rolling your own solution.
If nothing else suits your needs, you could pipe your HTTPUrlConnection stream to a file stream and write as you download. You would need to code the logic to manage the download state by yourself. First you would need to know in advance the size of the file to download (if your server sends you the "Content-Length" header), and to resume a partial failed download you would open a new connection to the same file and request from the last downloaded byte using http ranges.

How to upload a large file on android?

Recently I have been working on an app and it requires the files on the phone to be uploaded to the server. These file may be either images or videos. I used ASyncTask to do the networking on the background.
However if the file size is greater than 45 MBs the file upload fails...it works just fine other-wise
What should I use instead of Async Tasks? Should I go for Sync Adapters or for the Volley library? I know nothing in either of these.
You can use retrofit typedfile approach to upload file in multi-part.
For your reference :
https://futurestud.io/blog/retrofit-how-to-upload-files/
What response do you get from the server when the upload fails? Understanding the response can help you get insight into why the upload is failing. One possibility is that the server you are trying to upload file to is not configured to handle that big payload in which case you will get the following HTTP response.
HTTP Error 413 Request entity too large
HTTP Error 413
However this is just one of the possibilities. I suggest you to inspect HTTP response and it's other properties.

uploading files directly to server without web service from android

ok so i need to upload files directly to server without using any web service from android ..
first i am doing so with REST web service but the thing is it always say transaction is too large when executing my request .... in android i convert the image into byte[] then into Base64 string from my server side i decode this Base64 string and write bytes into file.
how can i upload directly to folder on my server ... something like executing a function that upload the file to "localhost/myWebApplicationDir/images/"then file here.jpg"
You will need to implement something that handles uploads as webservers typically don't do that out of the box. I'm sure you can find a POST upload script written in PHP somewhere on google. Then implement some code that POSTs the file to the endpoint and you should be fine.
There are no out-of-the-box tools for this but it's not a lot of work.
i found this link to be very helpful i discovered that you need an open connection with the server and communicate with streams something like keeping connection alive and upload files bit by bit.... but still you need a web service like #meredrica said.
http://ihofmann.wordpress.com/2013/01/23/android-sending-post-requests-with-parameters/

Multiple HTTP Connections (Segmented/Swarm Downloading) to download the same file in Android

I'm currently working on creating a Download Manager for Android. In order to optimize the download I need to download the same file using multiple connections to the server. (ie. the same technique used in Internet Download Manager used in Windows.)
The thing is I don't know how to create several HTTP connections and download the same file. I hope you can help me.. Thanks in advance!
Spawn off a few threads which request part of the file via the Header Content-Range keyword. see Reading the first part of a file using HTTP

Categories

Resources