Android Question Why does Try-Catch not work?

grafsoft

Well-Known Member
Licensed User
Longtime User
Hi,

this is from multipartpost.createpostrequest with a try-catch.
B4X:
     For i = 0 To Files.Size - 1 
       FD = Files.Get(i)
       Try
       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)
       In = File.OpenInput(FD.Dir, FD.FileName)
       File.Copy2(In, stream) 'read the file and write it to the stream ' herre the debugger stops
       b = EOL.GetBytes("UTF8")
       stream.WriteBytes(b, 0, b.Length)
       Catch
         Log (LastException.Message)
       End Try
     Next

The log:

** 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 **

logging fd.dir, fd.filename: both correct
/storage/emulated/0/Android/data/A.Tag/files/5
15.jpg
** Activity (upload) Resume **
An error occurred:
(Line: 30) FD = Files.Get(i)
java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Boolean

1. Why does try-catch not work?
2. What might be the reason?

Thank you all
Peter
 

Kevin

Well-Known Member
Licensed User
Longtime User
How are you dimming FD? Try/Catch is not working because the error is occurring outside of the Try/Catch block. ;)

It is having a problem assigning Files.Get(i) to FD.

An error occurred:
(Line: 30) FD = Files.Get(i)
java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Boolean
 
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
Thank you Kevin, you are right.
Fd is dimmed as filedata.Should be correct.

I was not dimmed at all. I added dim I as int.

B4X:
   Dim i As Int
   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)
       Try
       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)
       
       In = File.OpenInput(FD.Dir, FD.FileName)

Funny ...
 
Upvote 0

grafsoft

Well-Known Member
Licensed User
Longtime User
I can upload it, but I would have to send a database and some files to make it run.

And it does not make sense to build a project with only the essential parts, because then the error will not occur.

:(
 

Attachments

  • otto.zip
    83.1 KB · Views: 162
Upvote 0
Top