Android Tutorial Download huge files with HttpUtils2

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
Are you uploading files to a Http server? Which format does your server expect?
 

Erel

Administrator
Staff member
Licensed User
There isn't a single standard for uploading files to Http servers. The first step is to understand which format is expected by your server.
 

mjtaryan

Active Member
Licensed User
I'm unable to get this to work. I'm testing trying to download multiple files. I get no error messages and the log entries I've added indicate the files get downloaded. When I check the destination directory (for the test it is DirDefaultExternal), the files are there, but the device indicates they are somehow corrupted or empty.

I've tried to replace the HTTP library (Version 1.26) with the version provided in your link in the original post. However, as I indicated in a post in that thread, when the HTTP library finishes loading it continues to indicate Version 1.26. I don't know if this is the problem, but need to get it fixed in order to download data to my app.

The attached is the test project.
 

Attachments

Erel

Administrator
Staff member
Licensed User
You are not closing the output stream after calling File.Copy2.

There are other problems in your code as well. You should move all the non-UI global variables to process_globals and only load the data when FirstTime is True.

The timer is also not needed.
 

mjtaryan

Active Member
Licensed User
You are not closing the output stream after calling File.Copy2.

There are other problems in your code as well. You should move all the non-UI global variables to process_globals and only load the data when FirstTime is True.

The timer is also not needed.
Thanks Erel,

I completely forgot about closing the output stream.

I put this together quickly - mostly rearragning your code. I was trying to get a handle on how I'd handle downloading a group of files.

I changed the code as you indicated. The files now download, but there are still some problems.

1. I've downloaded three types of files -- jpg, txt and html. Although the files work in a test app I wrote, none of the commercial apps I tried appear to recognize them either for what they are or that they have content (size > 0).
2. I tried downloading the group using a loop and without the timer, but found that only the last file gets saved. It may be I'm not constructing the loop properly or the files are too small and get overwritten by the next before they're written out. I was having that problem prior to my original reply here. The timer makes the group download work and displays the progress sufficient for a large group of small files.

I started exploring this approach because I wasn't having success with the standard Http methods such as "GetString" for downloading an html file. Even when I tried doing that with a single file, it never got saved whereas the ordinary plain text file I tried did.

Any insights? Thanks again.
 

Erel

Administrator
Staff member
Licensed User
Can you upload your updated code?

2) The problem is not in GetString. The problem is somewhere in your code.
 

mjtaryan

Active Member
Licensed User
I apologize for giving the impression GetString was at fault. I was merely saying the html file wasn't writing to the destination directory. I started playing with the http utilities because in none of my programming, whether for mobile devices or other systems, have I had the need to download files from an off LAN/WAN location. That need has now arrived and I needed to learn how to successfully accomplish that.

I redid the original test with GetString and both the txt and html file get saved. However, both files may have illegal characters. But I don't know what they might be, which is why I haven't tried either Download2 or GetString2. I started exploring the large file download method in an attempt to avoid this issue. With the latter, all the files get downloaded. A minor drawback is that, whether using GetString or the bytestream method, the files can be displayed in my app but won't open in other commercial apps such as the device's browser (for the html file), a note editor (for the txt file) or the image viewer (for a jpg file). Like I say, that is a minor issue because I don't really intend for the downloaded files to be used except by the app I'm working on.

As you requested I've attached the original test. Thanks.
 

Attachments

Felix Maria

Member
Licensed User
U make life Easy....:)
The program downloaded even a 30mb file very quickly :)and the event triggred.
I tried to copy the file with the code
B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    Job.Release
    Dim o As OutputStream
    o = File.OpenOutput(File.DirDefaultExternal, "l.bal", False)
    File.Copy2(Job.GetInputStream, o)
    o.Close
End Sub
I am getting this error in the GetInputStream

java.io.FileNotFoundException: /data/data/b4a.example/cache/1: open failed: ENOENT (No such file or directory)

What could be the mistake?

Many Thanks.

Maria.
 

Erel

Administrator
Staff member
Licensed User
You should call Job.Release only after you copied the downloaded file.
 

Erel

Administrator
Staff member
Licensed User
Compare the actual number of bytes. Also remember that 1kb = 1024 bytes.
 

Kwame Twum

Active Member
Licensed User
Guys, is there a group of file types that are compatible with the DownloadService? Anytime I try to download files like .apk, .amr, 3gpp, etc with my app, I get "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. (Exception) Not initialized"
when I know they're right there on the server (#trust me, I put them there). Files like .txt, .zip, .mp3, .pdf and others however download nicely.
Could there be a problem? Thanks in advance!
 

Erel

Administrator
Staff member
Licensed User
The problem is not in HttpUtils2 or DownloadService (which don't know anything about the file downloaded). It is probably something with your server configuration.
 

Kwame Twum

Active Member
Licensed User
thanks Erel, I'll check that...
However, FileZilla on my computer is able to download all files without any problem, so I'm sure the problem is not coming from the server
 
Last edited:
Status
Not open for further replies.
Top