For new projects it is recommended to use OkHttpUtils2: https://www.b4x.com/android/forum/threads/54723/#content
Multipart POST requests are usually used by Html Form components to upload files and pairs of name/values to the server.
The attached code module makes it easy to create such requests.
The following code demonstrates the usage of this code module:
The method MultipartPost.CreatePostRequest does most of the job.
It accepts three parameters. The first is the URL (of the target, not the html page).
The second is a Map that contains the names and values pairs that will be sent to the server.
You can pass Null if it is not needed.
The third parameter is a List that holds FileData items.
Each FileData item represents a file that will be uploaded to the server.
Again, you can pass Null if it is not needed.
For example:
The above code will upload a file named 1.png from the assets folder (Files tab). The file will be mapped to "upfile" key.
Multipart POST requests are usually used by Html Form components to upload files and pairs of name/values to the server.
The attached code module makes it easy to create such requests.
The following code demonstrates the usage of this code module:
B4X:
Sub Process_Globals
Dim hc As HttpClient
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
hc.Initialize("hc")
End If
'Add files
Dim files As List
files.Initialize
Dim FD As FileData
fd.Initialize
fd.Dir = File.DirRootExternal
fd.FileName = "temp.apk"
fd.KeyName = "upfile2"
fd.ContentType = "application/octet-stream"
files.Add(fd)
'Add second file
Dim fd As FileData
fd.Initialize
fd.Dir = File.DirAssets
fd.FileName = "1.png"
fd.KeyName = "upfile"
fd.ContentType = "application/octet-stream"
files.Add(fd)
'Add name / values pairs (parameters)
Dim NV As Map
NV.Initialize
NV.Put("note1", "abc")
NV.Put("note2", "def")
Dim req As HttpRequest
req = MultipartPost.CreatePostRequest("http://www.example.com/1.php", NV, files)
hc.Execute(req, 1)
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("error: " & Response & " " & StatusCode)
If response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Msgbox(Response.GetString("UTF8"), "")
Response.Release
End Sub
It accepts three parameters. The first is the URL (of the target, not the html page).
The second is a Map that contains the names and values pairs that will be sent to the server.
You can pass Null if it is not needed.
The third parameter is a List that holds FileData items.
Each FileData item represents a file that will be uploaded to the server.
Again, you can pass Null if it is not needed.
For example:
B4X:
Dim fd As FileData
fd.Initialize
fd.Dir = File.DirAssets
fd.FileName = "1.png"
fd.KeyName = "upfile"
fd.ContentType = "application/octet-stream"
Attachments
Last edited: