Android Question Download Progress & OkHttpUtils2

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,

how can I get the download progress event with library okhttputils2 (when downloading huge files, for example?).
Anybody knows?
 

asales

Expert
Licensed User
Longtime User
Search in forum by "Download huge files with HttpUtils2" and you find a tutorial made by Erel.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
I already read that post and noticed that uses a very old tutorial (from 2013). As the new okhttputils2 library is updated in 2017, I think that we should use this one instead of that. The problem is when I replace the old okhttputils2 and httpjob modules in that sample I get an error - looks that the codes are incompatible.
At the end of the day, this means that if we use the update okhttputils2 library we cant use that code... even when using the modules from https://www.b4x.com/android/forum/threads/b4x-okhttputils2-ihttputils2-httputils2-source-code.82632/ there is a problem with that code.
Then, we have the question:
- If we have a fresh and new implementation and full recommended by @Erel okhttputils2 , we don't have any way to get the download progress of huge files? That is it?
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Here it is, it works great for me https://www.b4x.com/android/forum/t...he-android-downloadmanager-to-download.59840/

There are other solutions, but it works and it works great.

Screenshot_20180106-201452.jpg
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I already read that post and noticed that uses a very old tutorial (from 2013). As the new okhttputils2 library is updated in 2017, I think that we should use this one instead of that.
The example is based on OkHttp. The tutorial was updated multiple times since it was first written.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi @Erel . I noticed the updates but the fact is: at the end of the day, the final version of tutorial code is incompatible with the final version of okhttputils2 right? This means that if I have a project based in okhttputils2 will not be possible to reuse the code (the changes needed are huge because the events and methods are completely distinct...) That is it?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This example: https://www.b4x.com/android/forum/threads/download-huge-files-with-httputils2.30220/ is based on a slightly modified version of OkHttpUtils2.

The change is:
B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   ' ********** Modified code *************
   Dim cs As CountingOutputStream
   cs.Initialize(File.OpenOutput(TempFolder, TaskId, False))
   Dim j As HttpJob = TaskIdToJob.Get(TaskId)
   Dim jt As JobTag = j.Tag
   jt.CountingStream = cs
   jt.Total = Response.ContentLength
   If jt.Data.url = "" Then
       Log("Job cancelled before downloaded started")
       cs.Close
   End If
   Response.GetAsynchronously("response", cs , _
       True, TaskId)
   '**************************************
End Sub
Instead of:
B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
       True, TaskId)
End Sub

It should be simple to apply this modification to the latest version of OkHttpUtils2. The best way to do it is with code such as:
B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
Dim j As HttpJob = TaskIdToJob.Get(TaskId)
If j.Tag Is JobTag Then
 Dim cs As CountingOutputStream
 cs.Initialize(File.OpenOutput(TempFolder, TaskId, False))
 Dim jt As JobTag = j.Tag
   jt.CountingStream = cs
   jt.Total = Response.ContentLength
   If jt.Data.url = "" Then
       Log("Job cancelled before downloaded started")
       cs.Close
   End If
   Response.GetAsynchronously("response", cs , _
       True, TaskId)
   '**************************************
Else
 
 Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
       True, TaskId)
End Sub

This way it will work with both regular HttpJobs and jobs created with DownloadService.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Thank you so much @Erel . I have many codes those use okhttputils2 and would be very complicated to edit every line where I used it. I'll test to change the code...
 
Upvote 0
Top