httputils2 and headers

drgottjr

Expert
Licensed User
Longtime User
can i get the http headers from the server with this module?

if i'm expecting a bitmap (httpjob.getbitmap) and don't get it - eg, i get some html instead - i'm getting:
java.lang.RuntimeException: Object should first be initialized (Bitmap).
normally i would put: bm.Initialize3( Job.GetBitmap ), so i'm guessing the error is being thrown within the getbitmap sub.

i was going to try "try...catch", but i come from C so it isn't natural to me, and i'm not sure what it is i would be trapping for (the io was successful; it just sent the "wrong" string of bytes.)

then i thought, if i saw the server's "content-type" header, i would see that it wasn't an image, and i could skip the job or change it to deal with text.

i was helpfully advised on httpjob.getrequest.setheader on the way out. now i'm looking for httpjob.getrequest.getheader or httpjob.getrequest.getcontenttype on the way back. any suggestions there? thanks

-go
 

drgottjr

Expert
Licensed User
Longtime User
I don't see how the headers will help here. The string you see is an error message. It is not HTML. Can you post the code that handles the response (JobDone)?



here was the original code:
Sub JobDone( Job As httpjob )
ProgressDialogHide
If Job.Success = False Then
Msgbox("Sorry: (" & Job.JobName & ") " & Job.ErrorMessage,"")
Else
Select Job.JobName
Case "TheImg"
Dim bm As Bitmap
bm.Initialize3( Job.GetBitmap )
' do some stuff with bm then feed to imageview.bitmap
...


i never see the msgbox telling me the job.success = false, so that means success = true, right? i initialize3 the expected bitmap to job.getbitmap and continue on from there. yet i received an unitialized bitmap error message.
here's the way i look at it: the request for an image receives a code 200 from the server, but the server sends a burst of html text which i can see plainly in the log. so job.success = true and httputils2.getbitmap tries to loadbitmap from its temp folder. but what's in that file isn't a bitmap. bitmap.initialize3() doesn't offer me any return value. maybe i should test for null at that point? is that what job.getbitmap returns? the httputils2 documentation says it returns a bitmap. it doesn't say what it returns
in the event of failure. (to my mind, "returns a bitmap" means "returns a pointer to a bitmap struct, or null".) i don't know how b4a or java handle things like that.

in the meantime, and before you replied, i went ahead and changed my code to include a "try/catch", as shown below.

Sub JobDone( Job As httpjob )
ProgressDialogHide
If Job.Success = False Then
Msgbox("Sorry: (" & Job.JobName & ") " & Job.ErrorMessage,"")
Else
Select Job.JobName
Case "TheImg"
Dim bm As Bitmap
Try
bm.Initialize3( Job.GetBitmap )
Catch
Msgbox("Maybe this isn't an image...","")
Return
End Try
' do some stuff with bm
...


i think it may have had an effect. i don't see that error, and i keep testing. i also added try/catch in some other places where i access a bitmap which i had previously initialized3 and assumed was, in fact, a bitmap. there, too, try/catch stopped the error message. so maybe i answered my own question, yes? eg:
bm.Initialize3( Job.GetBitmap )
if bm = Null then


to clarify: the app generally works perfectly. this error was unexpected, especially since the job showed success = true and because i was initializing the bitmap. i did what i thought i needed to do. maybe the server should have sent code 400 or other error code. but apparently it didn't. and i wasn't ready for that.

i had wanted to see the http headers because if job.success = true but "content-type" was not "image/jpeg" or "image/...", i would use job.getstring instead of job.getbitmap to deal smoothly with unexpected content type instead of crashing.

thanks again for your time.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Please use [ code ] [ /code ] tags (without spaces) when posting code.

Bitmap.Initialize will fail if the file is not an image. Which link are you downloading? Usually the link is either an image or something else.


sorry about the missing tags. thanks for your comments. very helpful, as usual.
 
Upvote 0
Top