Hello to everyone,
please consider the following code. It is placed in a UI application. This code posts a file in a server with a multipart job (it is derived from an example of Erel) along with some name-value pairs. When I run it in debug mode I get response from "Wait For" in Sub GetTheResponse, but in release and release obfuscated mode, I get no response. What is wrong???? Here is the code:
please consider the following code. It is placed in a UI application. This code posts a file in a server with a multipart job (it is derived from an example of Erel) along with some name-value pairs. When I run it in debug mode I get response from "Wait For" in Sub GetTheResponse, but in release and release obfuscated mode, I get no response. What is wrong???? Here is the code:
B4X:
Public Sub GetTheResponse(assr As String, Directory As String, Filename As String, ActCase As Boolean)
'The following two are global variables
TempCounter = 0
TrackerIndex = 0
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "file"
fd.Dir = Directory
fd.FileName = Filename
Dim NameValues As Map
NameValues.Initialize
NameValues.Put("value1", "tp1resp")
NameValues.Put("assurance", assr)
Dim sURL As String
Select Case ActCase
Case False
sURL = "https://api.myserver.com/getdata1"
Case True
sURL = "https://api.myserver.com/getdata2"
End Select
Dim job As HttpJob = CreateMultipartJob(sURL, NameValues, Array(fd))
job.GetRequest.SetHeader("Authorization", $"Bearer 12312414ghv1c24h122421h3c1hc4g1h4g2214"$)
Log("1")
Wait For (job) JobDone (job As HttpJob)
Log("2")
Log(job)
Log(job.GetString)
File.Delete(xui.DefaultFolder, job.Tag)
job.Release
End Sub
Public Sub CreateMultipartJob(Link As String, NameValues As Map, Files As List) As HttpJob
Dim boundary As String = "---------------------------1461124740692"
TempCounter = TempCounter + 1
Dim TempFileName As String = "post-" & TempCounter
Dim stream As OutputStream = File.OpenOutput(xui.DefaultFolder, TempFileName, False)
Dim b() As Byte
Dim eol As String = Chr(13) & Chr(10)
Dim empty As Boolean = True
If NameValues <> Null And NameValues.IsInitialized Then
For Each key As String In NameValues.Keys
Dim value As String = NameValues.Get(key)
empty = MultipartStartSection (stream, empty)
Dim s As String = _
$"--${boundary}
Content-Disposition: form-data; name="${key}"
${value}"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Next
End If
If Files <> Null And Files.IsInitialized Then
For Each fd As MultipartFileData In Files
empty = MultipartStartSection (stream, empty)
Dim s As String = _
$"--${boundary}
Content-Disposition: form-data; name="${fd.KeyName}"; filename="${fd.FileName}"
Content-Type: ${fd.ContentType}
"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Dim in As InputStream = File.OpenInput(fd.Dir, fd.FileName)
File.Copy2(in, stream)
Next
End If
empty = MultipartStartSection (stream, empty)
s = _
$"--${boundary}--
"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Dim job As HttpJob
job.Initialize("", Me)
stream.Close
Dim length As Int = File.Size(xui.DefaultFolder, TempFileName)
Dim in As InputStream = File.OpenInput(xui.DefaultFolder, TempFileName)
Dim cin As CountingInputStream
cin.Initialize(in)
Dim req As OkHttpRequest = job.GetRequest
req.InitializePost(Link, cin, length)
req.SetContentType("multipart/form-data; boundary=" & boundary)
req.SetContentEncoding("UTF8")
TrackProgress(cin, length)
job.Tag = TempFileName
CallSubDelayed2(HttpUtils2Service, "SubmitJob", job)
Return job
End Sub
Private Sub MultipartStartSection (stream As OutputStream, empty As Boolean) As Boolean
If empty = False Then
stream.WriteBytes(Array As Byte(13, 10), 0, 2)
Else
empty = False
End If
Return empty
End Sub
Private Sub TrackProgress (cin As CountingInputStream, length As Int)
TrackerIndex = TrackerIndex + 1
Dim MyIndex As Int = TrackerIndex
Do While MyIndex = TrackerIndex
Log($"$1.2{cin.Count * 100 / length}%"$)
If cin.Count = length Then Exit
Sleep(100)
Loop
End Sub
Last edited: