S swabygw Active Member Licensed User Longtime User Mar 11, 2017 #1 I use the following code to download an image: Code: Copy Dim j As HttpJob j.Initialize("ImgDownload", Me) j.Download(ServerUrl & "media/" & $Filename) and, then, I use the following code to get the image: Code: Copy Dim bmp As Bitmap = Job.GetBitmap However, every once in a while, for a variety of other reasons, the file to be downloaded is missing or has been deleted by some other process. This causes the above Job.GetBitmap to throw an error which I handle and put a "image missing" image into variable bmp. I'd like to avoid the error entirely - is there a way to test if the Job really has a file or not? I've tried: Code: Copy If Job.GetBitmap = Null Then ... But this doesn't work. Is there another way?
I use the following code to download an image: Code: Copy Dim j As HttpJob j.Initialize("ImgDownload", Me) j.Download(ServerUrl & "media/" & $Filename) and, then, I use the following code to get the image: Code: Copy Dim bmp As Bitmap = Job.GetBitmap However, every once in a while, for a variety of other reasons, the file to be downloaded is missing or has been deleted by some other process. This causes the above Job.GetBitmap to throw an error which I handle and put a "image missing" image into variable bmp. I'd like to avoid the error entirely - is there a way to test if the Job really has a file or not? I've tried: Code: Copy If Job.GetBitmap = Null Then ... But this doesn't work. Is there another way?
S swabygw Active Member Licensed User Longtime User Mar 11, 2017 #2 I've also tried using the following instead: Code: Copy Dim links As Map links.Initialize links.Put(ImgVw, Starter.ServerUrl & "media/" & $Fiilename) CallSubDelayed2(ImageDownloader, "Download", links) which works great, but then when I call: Code: Copy Dim bmp As Bitmap bmp.Initialize(File.DirAssets, "image_missing.png") 'This is a backup image in case the real image is missing bmp = ImgVw.Bitmap 'This is the real image FitCenterBitmap(ImgVw, bmp) I get an error from the FitCenterBitmap function that variable bmp (the downloaded image) is not initialized - probably because it isn't downloaded yet. I like this method the best, but I must do a re-fit somehow, like FitCenterBitmap, and not sure how to do the re-fit. Upvote 0
I've also tried using the following instead: Code: Copy Dim links As Map links.Initialize links.Put(ImgVw, Starter.ServerUrl & "media/" & $Fiilename) CallSubDelayed2(ImageDownloader, "Download", links) which works great, but then when I call: Code: Copy Dim bmp As Bitmap bmp.Initialize(File.DirAssets, "image_missing.png") 'This is a backup image in case the real image is missing bmp = ImgVw.Bitmap 'This is the real image FitCenterBitmap(ImgVw, bmp) I get an error from the FitCenterBitmap function that variable bmp (the downloaded image) is not initialized - probably because it isn't downloaded yet. I like this method the best, but I must do a re-fit somehow, like FitCenterBitmap, and not sure how to do the re-fit.
S swabygw Active Member Licensed User Longtime User Mar 11, 2017 #3 I found a solution - go into the ImageDownloader code and use FitCenterBitmap in there. Sorry for the runaround...just had to talk it out a bit. Upvote 0
I found a solution - go into the ImageDownloader code and use FitCenterBitmap in there. Sorry for the runaround...just had to talk it out a bit.
KMatle Expert Licensed User Longtime User Mar 11, 2017 #4 swabygw said: I found a solution Click to expand... The server should respond a 404 error (not found) if the file isn't there. You can do a Job.GetString (this will not harm anything) and check if it contains 404 or so. As I use php I would check it in the script. The response is always a map (so you can check very easy if the key is f.e. "File" plus the file data as the value or if the key is "Not found" or similar. Upvote 0
swabygw said: I found a solution Click to expand... The server should respond a 404 error (not found) if the file isn't there. You can do a Job.GetString (this will not harm anything) and check if it contains 404 or so. As I use php I would check it in the script. The response is always a map (so you can check very easy if the key is f.e. "File" plus the file data as the value or if the key is "Not found" or similar.
S swabygw Active Member Licensed User Longtime User Mar 11, 2017 #5 Great idea, I should've thought of checking for a server error being returned - thanks! Upvote 0
S swabygw Active Member Licensed User Longtime User Mar 12, 2017 #6 @KMatle - I investigated your suggestion and I think that it is on the right path. However...in the ImageDownloader service, I can't figure out how to access the Response. What I mean is that I see the detailed 404 error in the log with a line that looks like this: ResponseError. Reason: Not Found, Response: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">...404.... And I want to test if the Response string contains string '404' (which it does). But the ImageDownloader uses HttpJob and I can't figure out how to get at the Response or the Reason. Can you advise? ImageDownloader here (https://www.b4x.com/android/forum/t...ple-way-to-download-images.30875/#post-179505) Upvote 0
@KMatle - I investigated your suggestion and I think that it is on the right path. However...in the ImageDownloader service, I can't figure out how to access the Response. What I mean is that I see the detailed 404 error in the log with a line that looks like this: ResponseError. Reason: Not Found, Response: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">...404.... And I want to test if the Response string contains string '404' (which it does). But the ImageDownloader uses HttpJob and I can't figure out how to get at the Response or the Reason. Can you advise? ImageDownloader here (https://www.b4x.com/android/forum/t...ple-way-to-download-images.30875/#post-179505)
KMatle Expert Licensed User Longtime User Mar 12, 2017 #7 As you can see, it uses Job.Done, too where you can add a Code: Copy IF Job.GetString.Contains("404") then 'not found ELSE 'do something with the image END IF The ImageDownloader just loads existing files (take a look) and has no logic if one isn't available as I can see. So just add it. Easier than it look's. I've done a small PHP script which retrieves all the files from a directory on the server: https://www.b4x.com/android/forum/t...r-directory-via-httputils2-php.43320/#content with a complete example. Maybe you are able to use it. Upvote 0
As you can see, it uses Job.Done, too where you can add a Code: Copy IF Job.GetString.Contains("404") then 'not found ELSE 'do something with the image END IF The ImageDownloader just loads existing files (take a look) and has no logic if one isn't available as I can see. So just add it. Easier than it look's. I've done a small PHP script which retrieves all the files from a directory on the server: https://www.b4x.com/android/forum/t...r-directory-via-httputils2-php.43320/#content with a complete example. Maybe you are able to use it.
S swabygw Active Member Licensed User Longtime User Mar 12, 2017 #8 I tried it in the JobDone in the ImageDownloader like this: But it never fired the "404" message. Btw, the "not 404" portion of that code does, indeed, fire. Last edited: Mar 12, 2017 Upvote 0
I tried it in the JobDone in the ImageDownloader like this: But it never fired the "404" message. Btw, the "not 404" portion of that code does, indeed, fire.
KMatle Expert Licensed User Longtime User Mar 12, 2017 #9 Please log Job.GetString and copy it here (just right click the logs window and select copy all - no need to upload images ) and we see what the server ist returning in that case. Upvote 0
Please log Job.GetString and copy it here (just right click the logs window and select copy all - no need to upload images ) and we see what the server ist returning in that case.
S swabygw Active Member Licensed User Longtime User Mar 12, 2017 #10 Yep, here it is. Thanks Wait, uploading a second file with the log of Job.GetString Attachments Document1.txt Document1.txt 6.7 KB · Views: 363 Upvote 0
S swabygw Active Member Licensed User Longtime User Mar 12, 2017 #11 Here it is. There are four files being downloaded. The first three download successfully - the fourth one is the one that is the missing image file. Attachments Document1.txt Document1.txt 13.3 KB · Views: 364 Upvote 0
Here it is. There are four files being downloaded. The first three download successfully - the fourth one is the one that is the missing image file.
KMatle Expert Licensed User Longtime User Mar 13, 2017 #12 There's a text " not found" the server returns. So just change 404 to "not found" Upvote 0
S swabygw Active Member Licensed User Longtime User Mar 13, 2017 #13 Yes, I'd like to - I see the 404 in the log in the Response field, but I can't get at the Reason or Response object, though, or retrieve the text. So, it's in the log, but I can't get at it with Job.GetString or any other command. Upvote 0
Yes, I'd like to - I see the 404 in the log in the Response field, but I can't get at the Reason or Response object, though, or retrieve the text. So, it's in the log, but I can't get at it with Job.GetString or any other command.
Erel B4X founder Staff member Licensed User Longtime User Mar 14, 2017 #14 Are you checking that Job.Success is true? It will be false if the status code is not 2xx. Upvote 0
S swabygw Active Member Licensed User Longtime User Mar 14, 2017 #15 Yes - the 404 gets logged before JobDone even fires. Here's the code and log file. You'll see in the log file that the "not Success" message comes after the 404 error, even though it's the first thing that happens in JobDone. Attachments Document1.txt Document1.txt 17.4 KB · Views: 389 Upvote 0
Yes - the 404 gets logged before JobDone even fires. Here's the code and log file. You'll see in the log file that the "not Success" message comes after the 404 error, even though it's the first thing that happens in JobDone.
DonManfred Expert Licensed User Longtime User Mar 14, 2017 #16 then you should check it in JobDone sub.... Code: Copy If success = false then ' Success = false. See errormessage for detail. ' no image available else ' Job Success... Get the image end if Upvote 0
then you should check it in JobDone sub.... Code: Copy If success = false then ' Success = false. See errormessage for detail. ' no image available else ' Job Success... Get the image end if
S swabygw Active Member Licensed User Longtime User Mar 14, 2017 #17 Thanks - I will go with that - the GetString method seems to work only on successful downloads (and retrieves the image binary data) but not on unsuccessful downloads (it causes an error). Out of curiosity, is there any way to access the Response and/or Reason property's (pictured in red in this image): (if not, it's okay, just thought that I'd try) Upvote 0
Thanks - I will go with that - the GetString method seems to work only on successful downloads (and retrieves the image binary data) but not on unsuccessful downloads (it causes an error). Out of curiosity, is there any way to access the Response and/or Reason property's (pictured in red in this image): (if not, it's okay, just thought that I'd try)
DonManfred Expert Licensed User Longtime User Mar 14, 2017 #18 In case of Success = false you should set a dummy image (a PictureNotFound.jpg for ex) swabygw said: is there any way to access the Response Click to expand... Code: Copy Sub JobDone(Job As HttpJob) If Job.Success = False Then Log($"Error occured: ${Job.ErrorMessage}"$) else If Job.Success Then End If Job.Release End Sub Upvote 0
In case of Success = false you should set a dummy image (a PictureNotFound.jpg for ex) swabygw said: is there any way to access the Response Click to expand... Code: Copy Sub JobDone(Job As HttpJob) If Job.Success = False Then Log($"Error occured: ${Job.ErrorMessage}"$) else If Job.Success Then End If Job.Release End Sub
S swabygw Active Member Licensed User Longtime User Mar 14, 2017 #19 Yep - that's exactly what I'm going to do. Thanks! Upvote 0
victormedranop Well-Known Member Licensed User Longtime User Mar 16, 2017 #20 Try ultimate list view. I use it to download a bunch of images. Amazing lib. Victor Upvote 0