Android Question Get progress upload OKHttpJob

devmobile

Active Member
Licensed User
I upload file to server with PostMultipart method in OKHttpJob library
I need to show progress for upload
How do can i show it?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is only relevant if the file is quite large.

You need to modify HttpJob.PostMultipart.
1. Add a global CountingInputStream variable to HttpJob.
2. Remove the call to PostBytes and instead send the request with this code:
B4X:
Dim in As InputStream
Dim data() As Byte = stream.ToBytesArray
in.InitializeFromBytesArray(data, 0, data.Length)
cin.Initialize(in) 'global CountingInputStream
req.InitializePost(Link, cin, data.Length)

You can now track the progress with a timer by checking Job.cin.Count.
 
Upvote 0

devmobile

Active Member
Licensed User
It is only relevant if the file is quite large.

You need to modify HttpJob.PostMultipart.
1. Add a global CountingInputStream variable to HttpJob.
2. Remove the call to PostBytes and instead send the request with this code:
B4X:
Dim in As InputStream
Dim data() As Byte = stream.ToBytesArray
in.InitializeFromBytesArray(data, 0, data.Length)
cin.Initialize(in) 'global CountingInputStream
req.InitializePost(Link, cin, data.Length)

You can now track the progress with a timer by checking Job.cin.Count.
Thanks but i use library no class
What is different Httpjob class and Httpjob library?
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
It is only relevant if the file is quite large.

You need to modify HttpJob.PostMultipart.
1. Add a global CountingInputStream variable to HttpJob.
2. Remove the call to PostBytes and instead send the request with this code:
B4X:
Dim in As InputStream
Dim data() As Byte = stream.ToBytesArray
in.InitializeFromBytesArray(data, 0, data.Length)
cin.Initialize(in) 'global CountingInputStream
req.InitializePost(Link, cin, data.Length)

You can now track the progress with a timer by checking Job.cin.Count.

Hi @Erel . What is "cin" ... where is its declaration ?

I added this as follows but really didn't find the "cin" type or declaration...

B4X:
Sub Class_Globals
    Public JobName As String
    Public Success As Boolean
    Public Username, Password As String
    Public ErrorMessage As String
    Private target As Object
    Public CountInputStream As Int
...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
I tried to do:

B4X:
Public Cin As InputStream

as the call req.InitializePost(Link, Cin, data.Length) requires an InputStream object but the InputSteran doesn't have Initialize method... It seems to be a stupid error but I really didn't find the correct type for Cin ... please help!
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Add reference to RandomAccessFile library.
Thanks for fast answering!
I added the reference... but still getting Unknown Member: initialize when adding Cin.Initialize(in) . Notice that Cin declaration is:

Public Cin As InputStream

I noticed also that if Cin is InputStream, then the correct call is InitializeFromBytesArray ... what means that Cin type shouldn't be InputStream... but, what is the correct Cin data type then???

upload_2018-5-22_15-22-10.png
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hi @Erel . What is "cin" ... where is its declaration ?
You should read what he wrote
It is only relevant if the file is quite large.

You need to modify HttpJob.PostMultipart.
1. Add a global CountingInputStream variable to HttpJob.
2. Remove the call to PostBytes and instead send the request with this code:
B4X:
Dim in As InputStream
Dim data() As Byte = stream.ToBytesArray
in.InitializeFromBytesArray(data, 0, data.Length)
cin.Initialize(in) 'global CountingInputStream
req.InitializePost(Link, cin, data.Length)
You can now track the progress with a timer by checking Job.cin.Count.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
B4X:
dim cis as CountingInputStream ' THIS is what Erel wrote/suggested above
Hi @DonManfred ... :D:D Thanks! You are my help from an eternal stupid loop... I knew that was simple !!! I tried to find the CountInputStream object BEFORE adding RandomAccessFileLibrary... and didn't tried again after!!! I really didn't notice that this object was dependent of this library.
Sometimes when we are programming we need an external help to see what we can't, maybe due the proximity (or caffeine excess, who knows!) - thanks again! You saved my day!
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
B4X:
dim cis as CountingInputStream ' THIS is what Erel wrote/suggested above
Ok... not yet :(
The code with PostBytes works fine (server receives file, JobDone event is raised). The code changed doesn't raise the JobDone event and the file is not uploaded to server. Any suggestion?

B4X:
    b = s.Replace(CRLF, eol).GetBytes("UTF8")
    stream.WriteBytes(b, 0, b.Length)
    'PostBytes(Link, stream.ToBytesArray)

    Dim in As InputStream
    Dim data() As Byte = stream.ToBytesArray
    in.InitializeFromBytesArray(data, 0, data.Length)
    Cin.Initialize(in) 'global CountingInputStream
    req.InitializePost(Link, Cin, data.Length)
    
    req.SetContentType("multipart/form-data; boundary=" & boundary)
    req.SetContentEncoding("UTF8")
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Any suggestion? PostBytes -> works ... InitializePost -> doesn't work... Hi @Erel , could help us here ? The possibility to use this implementation could maintain the same structure of simple job.postmultipart / jobdone ...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi @DonManfred ... :D:D Thanks! You are my help from an eternal stupid loop... I knew that was simple !!! I tried to find the CountInputStream object BEFORE adding RandomAccessFileLibrary... and didn't tried again after!!! I really didn't notice that this object was dependent of this library.
Sometimes when we are programming we need an external help to see what we can't, maybe due the proximity (or caffeine excess, who knows!) - thanks again! You saved my day!
Hi @DonManfred ... I added a network spy at server side... the request using req.InitializePost(Link, Cin, data.Length) (HttpRequest) doesn't arrive at server... and there is no any message in Android also..
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Finally, I discovered what happened. It was again a stupid detail, as I suspected. Not #ONLY# remove the call to PostBytes... It's needed to add at the end of new code the SubmitJob Call, of course ( :rolleyes: )... if you do not call the routine, the post is prepared but will not be executed...

Then, the code in MultiPartPost routine is:

B4X:
    'PostBytes(Link, stream.ToBytesArray)
   
    Dim in As InputStream
    Dim data() As Byte = stream.ToBytesArray
    in.InitializeFromBytesArray(data, 0, data.Length)
    Cin.Initialize(in) 'global CountingInputStream
    req.InitializePost(Link, Cin, data.Length)

    Log("length -> " & data.Length)
    req.SetContentType("multipart/form-data; boundary=" & boundary)
    req.SetContentEncoding("UTF8")
   
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)

I hope that this experience could be useful to somebody...

Thanks for all members those helped, specially @DonManfred and @Jorge M A
 
Upvote 0
Top