I implemented it like this:
Everything works, but from time to time I began to receive messages from users about transmission errors. For this I implemented a retransmission if the first time I received an error.
But I still get errors from time to time:
As I understand it, "Server Error (500)" is a server error and I can't do anything about it, but errors starting with "java" are errors in the program. Why can these errors come? I can assume that this may be due to periodic Internet freezes. Perhaps my sending code is not written quite correctly. How to correctly implement sending a set of photos to the server, while taking into account that the Internet can periodically freeze?
B4X:
Sub Post (Vin As String)
Dim templist As List
templist=File.ListFiles(File.DirInternal&Vin)
Dim testerr="" As String
Dim re=False As Boolean
For i=0 To templist.Size-1
Sleep(50)
Dim s=templist.Get(i) As String
If s.Contains(".jpg") Then
Service.StartForeground(1,CreateNotification("Photo "&i&" from "&(templist.Size-1)))
CallSub2(Main,"PDS","Photo "&i&" from "&(templist.Size-1))
Dim j As HttpJob
j.Initialize("", Me)
Dim basicAuth As String = "XXX:XXX"
Dim su As StringUtils
Dim authInfo = su.EncodeBase64(basicAuth.GetBytes("UTF8")) As String
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "image"
fd.Dir = File.DirInternal&Vin
fd.FileName = templist.Get(i)
j.PostMultipart("https://XXX", CreateMap("image": templist.Get(i)), Array(fd))
j.GetRequest.SetHeader("Authorization", "Basic "&authInfo)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
re=False
Else
re=True
Log("JobError: "&j.ErrorMessage)
testerr=testerr&j.ErrorMessage& Chr(13) & Chr(10)
End If
j.Release
If re Then
Dim j As HttpJob
j.Initialize("", Me)
Dim basicAuth As String = "XXX:XXX"
Dim su As StringUtils
Dim authInfo = su.EncodeBase64(basicAuth.GetBytes("UTF8")) As String
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "image"
fd.Dir = File.DirInternal&Vin
fd.FileName = templist.Get(i)
j.PostMultipart("https://XXX", CreateMap("image": templist.Get(i)), Array(fd))
j.GetRequest.SetHeader("Authorization", "Basic "&authInfo)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Else
Log("JobError: "&j.ErrorMessage)
testerr=testerr&"After re-sending, the error is still: "&j.ErrorMessage& Chr(13) & Chr(10)
End If
j.Release
End If
End If
Next
If testerr<>"" Then
CallSubDelayed2(Main,"Err",testerr)
End If
Service.StartForeground(1,CreateNotification("All photos transferred"))
CallSubDelayed2(Main,"Finish",Vin)
End Sub
But I still get errors from time to time:
B4X:
java.net.SocketTimeoutException: timeout
java.net.UnknownHostException: Unable to resolve host "XXX": No address associated with hostname
Server Error (500)