Android Tutorial Download huge files with HttpUtils2

Discussion in 'Tutorials & Examples' started by Erel, Jun 13, 2013.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    Do you get this error with the unmodified code as well?
     
  2. aidymp

    aidymp Active Member Licensed 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!
     
  3. Erel

    Erel Administrator Staff Member Licensed 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.

    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.
     
  4. aidymp

    aidymp Active Member Licensed User

    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??

    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!
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Assuming that Job.Success is true then the request has completed and the response is stored in a temporary file. You should use the Job methods to access the response.
     
  6. ibra939

    ibra939 Active Member Licensed User

    Thanks you Erel
     
  7. ibra939

    ibra939 Active Member Licensed 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 ?
     
  8. ibra939

    ibra939 Active Member Licensed User

    how can i use file.copy2 ? to find my download file ?
     
  9. Inman

    Inman Well-Known Member Licensed 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.
    Code:
    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?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    ibra939 likes this.
  11. Inman

    Inman Well-Known Member Licensed User

    It is in the DownloadService which I downloaded from here. I don't remember making any changes to the code. Here is the code.
    Code:
    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
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    You shouldn't modify DownloadService code. You need to use its methods as done in the example.
     
  13. Inman

    Inman Well-Known Member Licensed User

    I didn't modify. I used the following code from example.
    Code:
    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.
     
  14. ibra939

    ibra939 Active Member Licensed User

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

    Erel Administrator Staff Member Licensed 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.
     
  16. ibra939

    ibra939 Active Member Licensed User


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

    Erel Administrator Staff Member Licensed User

    It is not so simple. Especially not in the general case. You need to go over HttpUtils2Service and see how it stores the file.
     
    ibra939 likes this.
  18. ibra939

    ibra939 Active Member Licensed User

    thanks Erel so much i will try
     
  19. sasidhar

    sasidhar Active Member Licensed User

     
  20. DonManfred

    DonManfred Expert Licensed User

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

    See this example

    Code:
    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
    Code:
    Sub GetFilename(fullpath As StringAs String
       
    Return fullpath.SubString(fullpath.LastIndexOf("/") + 1)
    End Sub
     
    incendio and ibra939 like this.
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice