Android Tutorial Download huge files with HttpUtils2

Status
Not open for further replies.
Better to use: https://www.b4x.com/android/forum/threads/simple-progress-http-download.127214/#post-796463

The attached project includes a slightly modified version of HttpUtils2 and a new service named DownloadService. The purpose of DownloadService is to make it simple to download files of any size.

SS-2013-06-13_17.34.35.png


It is very simple to use this service.

Start a download:
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

Handle the events:
B4X:
Sub dd_Progress(Progress As Long, Total As Long)
   ProgressBar1.Progress = Progress / Total * 100
   Label1.Text = NumberFormat(Progress / 1024, 0, 0) & "KB / " & _
      NumberFormat(Total / 1024, 0, 0) & "KB"
End Sub

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

Cancel a download:
B4X:
Sub btnCancel_Click
   CallSubDelayed2(DownloadService, "CancelDownload", link1)
End Sub

DownloadService allows you to download multiple files at once and track the progress of each one of them.

The following libraries are required:
OkHttp
StringUtils
Phone (required in order to acquire a partial lock during the download)
RandomAccessFile

As this is a modified version of OkHttpUtils2 you should not reference OkHttpUtils2 library. You should instead add the three modules from the attached project. Note that the modified code is in Sub hc_ResponseSuccess.
 

Attachments

  • LargeFileDownload.zip
    11.5 KB · Views: 2,874
Last edited:

incendio

Well-Known Member
Licensed User
Longtime User
As this is a modified version of HttpUtils2 you should not reference HttpUtils2 library. You should instead add the three modules from the attached project.

Hi guys,

I used RDC in my app, without HttpUtils2 library, RDC doesn't work, is it possible to make download & RDC works?

Thanks in advance.
 
Last edited:

incendio

Well-Known Member
Licensed User
Longtime User
I got error message like this :
Unfortunately myapp has closed.

Myapp just quit. It was happen on login, which I use RDC to load user name & password from remote server.
 

incendio

Well-Known Member
Licensed User
Longtime User
I renamed HttpJob and HttpUtils2Service, RDC worked fine, but there are 2 errors
1) Downloading running but Progress bar and label that shows downloaded size was not updated
2) After Downloading running for a while, it raised an error : "Unfortunately myapp has stopped"

Downloading is point to dropbox. Use friend phone to test it, but this device not attached to pc, so don't know the logs.

My tablet is attached to PC but for a strange reason, this tablet can't even connect to www.dropbox.com, thats why, tested on friend phone.

If only running your example (without HttpUtils2 library) on phone, it was ok. I guest it was conflict with HttpUtils2 library.
 

ibra939

Active Member
Licensed User
Longtime User
B4X:
Dim out As OutputStream = File.OpenOutput(...) 'target folder
File.Copy2(Job.GetInputStream, out)
out.Close


Erel this for saving the file or URL ?
 

ibra939

Active Member
Licensed User
Longtime User
Problem attached file
 

Attachments

  • Problems.zip
    11.6 KB · Views: 489
  • Screenshot_2014-10-25-21-44-25.png
    Screenshot_2014-10-25-21-44-25.png
    104.8 KB · Views: 461

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your dd_complete code is wrong.

It should be:
B4X:
Sub dd_Complete(Job As HttpJob)
   Log("Job completed: " & Job.Success)
   If Job.Success Then
   Dim OutStream As OutputStream
  Log("DownloadReady: "&Job.Tag)
  OutStream = File.OpenOutput(File.DirRootExternal, Job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
  File.Copy2(Job.GetInputStream,OutStream) ' save the file
  OutStream.Close
 End IF
Job.Release
End Sub
 

ibra939

Active Member
Licensed User
Longtime User
Your dd_complete code is wrong.

It should be:
B4X:
Sub dd_Complete(Job As HttpJob)
   Log("Job completed: " & Job.Success)
   If Job.Success Then
   Dim OutStream As OutputStream
  Log("DownloadReady: "&Job.Tag)
  OutStream = File.OpenOutput(File.DirRootExternal, Job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
  File.Copy2(Job.GetInputStream,OutStream) ' save the file
  OutStream.Close
End IF
Job.Release
End Sub

thanks for your help Erel ....
 

ibra939

Active Member
Licensed User
Longtime User
, IsInitialized=true]: open failed: ENOENT (No such file or directory) mp3 file Link
 

ibra939

Active Member
Licensed User
Longtime User
, IsInitialized=true]: open failed: ENOENT (No such file or directory) mp3 file Link
You should post the full error message and the relevant code.
but the size for the file 850kb .....not working donload as i test pdf file

** Activity (main) Pause, UserClosed = false **


** Activity (main) Resume **


** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


** Service (downloadservice) Create **


** Service (downloadservice) Start **


** Service (httputils2service) Create **


** Service (httputils2service) Start **


false


java.io.FileNotFoundException: /storage/sdcard0/Android/data/B.altoki/files/[CountingStream=(MyOutputStream) anywheresoftware.b4a.randomaccessfile.CountingStreams$MyOutputStream@414aea70, Data=[EventName=dd, url=http:/moysar.com/download/molay.mp3, Target=class B.altoki.main


, IsInitialized=false], Total=880768
, IsInitialized=true]: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:460)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:370)
at B.altoki.main._dd_complete(main.java:378)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:952)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)


at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:444)
... 16 more
java.io.FileNotFoundException: /storage/sdcard0/Android/data/B.altoki/files/[CountingStream=(MyOutputStream) anywheresoftware.b4a.randomaccessfile.CountingStreams$MyOutputStream@414aea70, Data=[EventName=dd, url=http:/moysar.com/download/molay.mp3, Target=class B.altoki.main
, IsInitialized=false], Total=880768
, IsInitialized=true]: open failed: ENOENT (No such file or directory)
** Activity (main) Pause, UserClosed = true **
 

Attachments

  • Problems.zip
    11.6 KB · Views: 458
Last edited:

Cableguy

Expert
Licensed User
Longtime User
aren't you missing an extra '/' here?

....http://moysar.com/download/molay.mp3...
 

Cableguy

Expert
Licensed User
Longtime User
for the day I cannot help you much. I will try to help you with this tomorow.
in the meanwhile, try to create a paralel simpler project just to accomplish file download... then you can start building on it...
 

ibra939

Active Member
Licensed User
Longtime User
for the day I cannot help you much. I will try to help you with this tomorow.
in the meanwhile, try to create a paralel simpler project just to accomplish file download... then you can start building on it...
:rolleyes:o_O:eek: Playing working fine but downloading i tried to do it .............
 

Cableguy

Expert
Licensed User
Longtime User
have you tried to download via browser in a pc? just to make sure the file is not download protected...
 

ibra939

Active Member
Licensed User
Longtime User
have you tried to download via browser in a pc? just to make sure the file is not download protected...

browser working fine
 

Attachments

  • Screenshot_2014-10-25-21-44-05.png
    Screenshot_2014-10-25-21-44-05.png
    87.1 KB · Views: 435
Status
Not open for further replies.
Top