Hi,
I want to upload a list of files. The program crashes at the first file, but the file exists.
This is how I add it to the list, like the demo:
This is my output:
(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=A.Tag/.main }
no extras
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (upload) Create, isFirst = true **
18
http://www.grafsoft.at/a-tag/multipartpost.php
/storage/emulated/0/Android/data/A.Tag/files/5
15.jpg
** Activity (upload) Resume **
An error occurred:
(Line: 41) b = EOL.GetBytes("UTF8")
java.lang.NullPointerException
The code that crashes at file.copy2:
What can I do?
Thanks
Peter
I want to upload a list of files. The program crashes at the first file, but the file exists.
This is how I add it to the list, like the demo:
B4X:
Dim fd As FileData
fd.Initialize
fd.FileName=Main.thepic
fd.Dir=Main.outdir
fd.KeyName = "Pic"
fd.ContentType = "application/octet-stream"
files.Add(fd)
This is my output:
(Intent) Intent { act=android.intent.action.MAIN flg=0x20000000 cmp=A.Tag/.main }
no extras
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (upload) Create, isFirst = true **
18
http://www.grafsoft.at/a-tag/multipartpost.php
/storage/emulated/0/Android/data/A.Tag/files/5
15.jpg
** Activity (upload) Resume **
An error occurred:
(Line: 41) b = EOL.GetBytes("UTF8")
java.lang.NullPointerException
The code that crashes at file.copy2:
B4X:
Dim NV As Map
NV.Initialize
NV.Put("action", "upload")
Dim req As HttpRequest
Log (files.Size)
Log (Main.hostname & "multipartpost.php")
' here it crashes
req = MultipartPost.CreatePostRequest(Main.hostname & "multipartpost.php", NV, files)
Sub CreatePostRequest(URL As String, NameValues As Map, Files As List) As HttpRequest
Dim boundary As String
boundary = "---------------------------1461124740692"
Dim stream As OutputStream
stream.InitializeToBytesArray(20)
Dim EOL As String
EOL = Chr(13) & Chr(10) 'CRLF constant matches Android end of line character which is chr(10).
Dim b() As Byte
If NameValues <> Null AND NameValues.IsInitialized Then
'Write the name/value pairs
Dim key, value As String
For i = 0 To NameValues.Size - 1
key = NameValues.GetKeyAt(i)
value = NameValues.GetValueAt(i)
b = ("--" & boundary & EOL & "Content-Disposition: form-data; name=" _
& QUOTE & key & QUOTE & EOL & EOL & value & EOL).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Next
End If
If Files <> Null AND Files.IsInitialized Then
'write the files
Dim FD As FileData
For i = 0 To Files.Size - 1
FD = Files.Get(i)
b = ("--" & boundary & EOL & "Content-Disposition: form-data; name=" _
& QUOTE & FD.KeyName & QUOTE & "; filename=" & QUOTE & FD.FileName & QUOTE _
& EOL & "Content-Type: " & FD.ContentType & EOL & EOL).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Dim In As InputStream
Log (FD.Dir)
Log (FD.FileName)
' crashes here
In = File.OpenInput(FD.Dir, FD.FileName)
File.Copy2(In, stream) 'read the file and write it to the stream
b = EOL.GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Next
End If
b = (EOL & "--" & boundary & "--" & EOL).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
b = stream.ToBytesArray
'msgbox(b.Length, "")
Dim request As HttpRequest
request.InitializePost2(URL, b)
request.SetContentType("multipart/form-data; boundary=" & boundary)
request.SetContentEncoding("UTF8")
Return request
End Sub
What can I do?
Thanks
Peter