Android Question HttpUtils2 Upload Progress?

rafaelmotaquintana

Active Member
Licensed User
I've found a download progress function for httputils2. It works fine.
Is there any upload progress function? I've searched and found nothing.
 

rafaelmotaquintana

Active Member
Licensed User
I'm modifying this lib published
https://www.b4x.com/android/forum/threads/work-with-google-drive.58805/

I managed to insert the download progress.
This is the httputils2service

B4X:
Type=Service
Version=2.00
StartAtBoot=False
'HttpUtils2 version 2.00
'Service module
Sub Process_Globals
    Private hc As HttpClient
    Private TaskIdToJob As Map
    Public TempFolder
    Private taskCounter As Int
    Private contentLength As Long
    Private tmr As Timer
    Private filename As Int
    Private downloadedLength As Long
    Public target As Object
    Public progressSub As String
    Public progresscancel As String
    Public timerInterval As Int
    Private wevmodule As Object
    Type ProgressStatus(Downloaded As Long,Total As Long)
End Sub

Sub Service_Create
    TempFolder = File.DirInternalCache
    hc.Initialize("hc")
    tmr.Initialize("tmr",timerInterval)
   
    TaskIdToJob.Initialize
End Sub

Sub tmr_Tick
    If     File.Exists(TempFolder, filename)=True Then
        downloadedLength=    File.Size(TempFolder, filename)
    End If
    Dim tmp As ProgressStatus
    tmp.Downloaded=downloadedLength
    tmp.Total=contentLength
    CallSub2(target,progressSub,tmp)
End Sub

Sub Service_Start (StartingIntent As Intent)
   
End Sub

Sub Service_Destroy

End Sub

Public Sub SubmitJob(job As HttpJob2, evmodule As Object) As Int
    taskCounter = taskCounter + 1
    wevmodule = evmodule
    TaskIdToJob.Put(taskCounter, job)
    If job.Username <> "" And job.Password <> "" Then
        hc.ExecuteCredentials(job.GetRequest, taskCounter, job.Username, job.Password)
    Else
        hc.Execute(job.GetRequest, taskCounter)
    End If
    Return taskCounter
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    contentLength = Response.contentLength
    If progressSub <> "" Then
        filename = TaskId
        tmr.Enabled = True
    End If
    Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), _
        True, TaskId)
End Sub

Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
    If Success Then
        CompleteJob(TaskId, Success, "")
    Else
        CompleteJob(TaskId, Success, LastException.Message)
        CallSubDelayed2(target,progresscancel,LastException.Message)
    End If
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    If Response <> Null Then
        Try
            Log(Response.GetString("UTF8"))
        Catch
            CallSub2(target,progresscancel,"")
            Log("Failed to read error message.")
        End Try
        Response.Release
    End If
    CompleteJob(TaskId, False, Reason)
End Sub

Sub CompleteJob(TaskId As Int, success As Boolean, errorMessage As String)
    Dim job As HttpJob2
    job = TaskIdToJob.Get(TaskId)
    TaskIdToJob.Remove(TaskId)
    job.success = success
    job.errorMessage = errorMessage
    job.Complete(TaskId)
    If progressSub <> "" Then
        tmr.Enabled = False
        Dim tmp As ProgressStatus
        tmp.Downloaded=contentLength
        tmp.Total=contentLength
        CallSub2(target,progressSub,tmp)
    End If
    If success = False Then
        CallSub2(target,progresscancel,"")
    End If
End Sub
 
Upvote 0
Top