Android Tutorial Download huge files with HttpUtils2

Status
Not open for further replies.

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, First off this is my first post on the forum, I purchased a couple of days ago! My only experience in programming was AMOS Basic on the Commodore Amiga, And a breif attempt in VB on windows 3.11! So I wanted to try my hand but had a look at this JAVA stuff, and to be honest even the IDE scared me! lol, So many files for such small tasks! I found this Basic4Android And Wow! I have made my first app and launched it! works really well! Thanks for making Basic4Android! I Love it!

So to the question! I know A little basic, and my App is basically the largefilesdownload example but with added stuff! But I dont understand a few things! in this example below

Sub dd_Complete(Job As HttpJob)
Log("Job completed: " & Job.Success)
Job.Release
End Sub

Q. After the job is released, how do I know that the download was successful?

Q. Log, Where is it? How do I read it?

Thanks once again for such a superb and easy to use, IDE and language!
 

aidymp

Well-Known Member
Licensed User
Longtime User
You should work with the job in this sub, before it is released. I recommend you to go over HttpUtils2 tutorial to understand how HttpJob works.

I have a few times but im still very new, lol. So the fact that the Job has A) downloaded and B) Saved the file should be saved to some global variables that I can check outside the sub? is that about right??

The logs appear in the Logs tab at the right side of the IDE. If you are using B4A-Bridge then they will only appear in debug mode.

Oh! lol I was looking on the device for a log file! Silly me!

Thanks and expect a lot more quiestions in the coming years!

Greatly appreticate this program, and your AMAZING forum!
 

ibra939

Active Member
Licensed User
Longtime User
Thanks you Erel
 

ibra939

Active Member
Licensed User
Longtime User
is it current ?
1- i did button code as follow :

Sub btnCancel_Click
'If Select
' Case:1
CallSubDelayed2(DownloadService, "CancelDownload", link1)
' Case:2
CallSubDelayed2(DownloadService, "CancelDownload", link2)

'End If
End Sub


---------------------------------------------


2- all button :


Sub Button2_Click
Dim dd As DownloadData
dd.url = link3
dd.EventName = "dd"
dd.Target = Me
CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub

Sub Button1_Click
Dim dd As DownloadData
dd.url = link1
dd.EventName = "dd"
dd.Target = Me
CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub


--------------------------------------------------------


3 - link

Sub Process_Globals
Private link1 As String = "http://example.com/a.mp3"
Private link2 As String = "http://example.com/b.mp3"



the problem when i download is working but i can't find the file ?
can i get help ? is there any problem in code ?
 

ibra939

Active Member
Licensed User
Longtime User
The file is downloaded to a temporary folder. You should use File.Copy2 together with Job.GetInputStream to copy the file.
how can i use file.copy2 ? to find my download file ?
 

Inman

Well-Known Member
Licensed User
Longtime User
I am using DownloadService to download an html file. After downloading it, I parse the file, remove some unnecessary code and display the rest in a webview. The reason I am using DownloadService and not HttpUtils directly is that I need to monitor the progress of the download and display it using a progressbar.

I am calling this particular activity that downloads and displays the HTML page as StoryView. The first time I start this StoryView activity and initiate a download using DownloadService, nothing happens. No progress data, nothing. In the log I see this message.
B4X:
sending message to waiting queue of uninitialized activity (startdownload)

If I close this activity, get back to main and open this activity again, everything works fine. It will continue to work until the DownloadService is stopped by the system. Once that happens, the issue repeats.

I believe the issue happens every time the service is started. What could be wrong?
 

Inman

Well-Known Member
Licensed User
Longtime User
@Inman where is StartDownload declared? Can you post the code that starts the download?

It is in the DownloadService which I downloaded from here. I don't remember making any changes to the code. Here is the code.
B4X:
Public Sub StartDownload(data As DownloadData)
    If jobs.ContainsKey(data.url) Then
        Log("Ignoring duplicate request.")
        Return
    End If
    Dim J As HttpJob
    J.Initialize(data.url, Me)
    Dim tag As JobTag
    tag.Initialize
    tag.data = data
    J.tag = tag
    jobs.Put(data.url, J)
    J.Download(data.url)
    If timer1.Enabled = False Then StartTimer(data.Target)
End Sub
 

Inman

Well-Known Member
Licensed User
Longtime User
I didn't modify. I used the following code from example.
B4X:
Sub btnDownload_Click
   Dim dd As DownloadData
   dd.url = link1 '<--- download link
   dd.EventName = "dd"
   dd.Target = Me
   CallSubDelayed2(DownloadService, "StartDownload", dd)
End Sub

And surprisingly it works fine from the second time with the same code. The error only happens when IsPaused(DownloadService)=True.
 

ibra939

Active Member
Licensed User
Longtime User

working but u can't save file in download file will be good ???
 

ibra939

Active Member
Licensed User
Longtime User
HttpUtils2 saves the file to a temporary folder. You will need to modify it if you want to save it to a different location directly.


how i can change the location if i want save directly in download file ...?
 

ibra939

Active Member
Licensed User
Longtime User
thanks Erel so much i will try
 

sasidhar

Active Member
Licensed User
Longtime User
 

DonManfred

Expert
Licensed User
Longtime User
Where the downloaded data shall be stored..

Wherever you want... You must save them in Sub dd_Complete

See this example

B4X:
Sub dd_Complete(Job As HttpJob)
    Log("Job completed: " & Job.Success)
    If Job.Success Then
        Dim tag As JobTag = job.Tag
        Dim dd As DownloadData = Tag.Data
        Log("Download of "&GetFilename(dd.url)&" finished...")
    Dim OutStream As OutputStream
    OutStream = File.OpenOutput(File.DirRootExternal, GetFilename(dd.url), False)
        File.Copy2(Job.GetInputStream,OutStream) ' save the file
       OutStream.Close
       Log(GetFilename(dd.url)&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)
    End If
    Job.Release
End Sub

Edit:
sorry i forgot a used sub of mine...

Add this sub too
B4X:
Sub GetFilename(fullpath As String) As String
   Return fullpath.SubString(fullpath.LastIndexOf("/") + 1)
End Sub
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…