Android Question OkHttp - Multipart Upload Progressbar

thisismrb

Member
I looked around but couldn't find anything about Progressbar for uploading a file via Okhttp-Multipart.
We have one for Huge Download but nothing for uploading.

Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to use OkHttpUtils2 source.

Add a PostInputStream sub with this code to HttpJob:
B4X:
Public Sub PostInputStream(Link As String, In As InputStream, Length As Long)
        req.InitializePost(Link, In, length)
        CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

This will allow you to pass a CountingInputStream and monitor the progress with a timer.
 
Upvote 0

thisismrb

Member
You will need to use OkHttpUtils2 source.

Add a PostInputStream sub with this code to HttpJob:
B4X:
Public Sub PostInputStream(Link As String, In As InputStream, Length As Long)
        req.InitializePost(Link, In, length)
        CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

This will allow you to pass a CountingInputStream and monitor the progress with a timer.
Thank you.
Almost all pieces work fine except the
B4X:
Sub timer1_tick
    Log("TICK")
    For Each job As HttpJob In jobs.Values
        Dim jt As JobTag = job.Tag
        If jt.CountingStream.IsInitialized Then
            CallSub3(jt.Data.Target, jt.Data.EventName & "_Progress", _
                jt.CountingStream.Count, jt.Total)
        End If
    Next
End Sub
I don't where I should initialize it again. Because once I initialize CountingStream inside the HttpJob. Hence, It should be fine. However, I got the following error:
B4X:
java.lang.RuntimeException: Object should first be initialized (CountingInputStream).

Thank you for your time Erel.
Rëza
 
Last edited:
Upvote 0

thisismrb

Member
Why? You should only add a PostInputStream as I wrote above.

Just pass an CountingStream to Job.PostInputStream. No need to make any other change.

Because I got "java.io.EOFException" and nothing uploaded. While when I used your Huge Download code the upload part was fine. I changed the "HttpJob PostMultipart", removed the "PostBytes" and added following :

HttpJob:
Public in As InputStream
    Dim data() As Byte= stream.ToBytesArray
    in.InitializeFromBytesArray(data,0,data.Length)
    cin.Initialize(in)
    req.InitializePost(Link,cin,data.Length)

"cin" is global and defined as a data type:
B4X:
Type JobTag (Data As UploadData,  _
        cin As CountingInputStream, Total As Long)
. So, When I compare the codes, you defined it as follows while I don't have this section and I think I have to define it here and pass "Total" and "Count" to my upload service:

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
 
Upvote 0

thisismrb

Member
The upload part has nothing to do with the "huge download" example.

You just need to pass a CountingInputStream to Job.PostInputStream.


Thank you.

Yes, both of them working fine, PostInputStream or req.InitializePost(Link, in, Length) and pass CountingInputStream to them.

For Future reference/users:
I defined two more subs inside the HttpJob for getting Total and Count and then called them from my upload service.
 
Last edited:
Upvote 0
Top