Android Question Httputils2 issue when pulling an image that isn't there

boastrike

Member
Licensed User
Longtime User
I am experiencing a problem downloading product images.

Our solution uses images from a website located at a URL path that ends in the item number. There are thousands of item numbers and images and we don't know in advance if the image actually exists at the provided path as we don't manage the images.

The majority of calls to download a product image succeed without issue. However, there are cases which nothing exists at the provided path.

In a case where nothing exists at the URL path, one of two things happens. Sometimes, the try catch statements around the block of code setting the image in JobDone, fires and there are no issues; a placeholder image indicating the image could not be found is loaded. However, sometimes, even when downloading the same image, there is some issue and the application closes with a message indicating the application stopped working.

I have attempted to debug the issue, however, the debugger does not fire; the issue may be in an async process that does not handle it in a way the debugger can catch, of course there may be other explanations to the issue as well. It seems the issue is in the HttpJob or HttpUtils2 itself.

We are trying to keep the the application from closing if an image is not there. There are try catches in the JobDone sub, as well as a check too see if the HttpJob was successful (200....) Please provide feedback on how we may resolve the issue. I can look over HttpJob or HttpUtils2 if required. I know they used to be modules where we could see the code. Any feedback is welcome.

Thanks for your time! Boa
 

boastrike

Member
Licensed User
Longtime User
This is the code that instantiates and initializes the HttpJob
B4X:
Dim imagepath As String
                        imagepath = "http://www.europasports.com/media/catalog/250/" & edtItem.Text & ".jpg"
                        Try
                            Dim jobDownloadImage As HttpJob
                              jobDownloadImage.Initialize("jobDownloadImage",Me)
                            jobDownloadImage.Download(imagepath)
                        Catch
                            Dim rsie As RSImageEffects
                            Dim adHoc As Bitmap
                            adHoc.Initialize(File.DirAssets,"no_image.jpg")
                            PanelQtyImage.Bitmap = rsie.RoundCorner(adHoc, 20)
                        End Try

Here is the code from JobDone that handles the request.

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    ProgressDialogHide
    If Job.Success = True Then
        Select Job.JobName
Case "jobDownloadImage"
                Try
                    Dim rsie As RSImageEffects
                    PanelQtyImage.Bitmap = rsie.RoundCorner(Job.GetBitmap, 20)
                Catch
                    Dim rsie As RSImageEffects
                    Dim adHoc As Bitmap
                    adHoc.Initialize(File.DirAssets,"no_image.jpg")
                    PanelQtyImage.Bitmap = rsie.RoundCorner(adHoc, 20)
                End Try
        End Select

For the edtItem.Text in imagepath

works -> 851780003770 OR https://www.europasports.com/media/catalog/250/851780003770.jpg as imagepath

breaks -> 081950001958 OR https://www.europasports.com/media/catalog/250/081950001958.jpg as imagepath
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
There is a message coming back from the server (not found = 404) and Job.Success is false

<body>

<h1>Object not found!</h1>

<p>





The requested URL was not found on this server.






If you entered the URL manually please check your

spelling and try again.






</p>

<p>

If you think this is a server error, please contact

the <a href="mailto:postmaster@localhost">webmaster</a>.



</p>



<h2>Error 404</h2>

<address>

<a href="/">192.168.178.21</a><br />

<span>Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3</span>

</address>

</body>

</html>



JobName = GetImage, Success = false
Error: Not Found

B4X:
If Job.Success = True Then
    ...
Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
      Msgbox(Job.jobname & ": " & Job.ErrorMessage,"Error")

   End If
   Job.Release

In "else" = Job fails you can check it and decide what to do. Parse "Job.ErrorMessage" for 404.

There is no need to try&catch the send of a job (this works anyway).

A missing image sounds like a logic problem.
 
Upvote 0

KZero

Active Member
Licensed User
Longtime User
There is a message coming back from the server (not found = 404) and Job.Success is false



B4X:
If Job.Success = True Then
    ...
Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
      Msgbox(Job.jobname & ": " & Job.ErrorMessage,"Error")

   End If
   Job.Release

In "else" = Job fails you can check it and decide what to do.

There is no need to try&catch the send of a job (this works anyway).

the httputils2service crashes before firing Jobdone


B4X:
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
Create Finished
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Failed to read error message.
java.lang.RuntimeException: android.os.NetworkOnMainThreadException
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA$3.run(BA.java:332)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5752)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
    at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:739)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
    at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:220)
    at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:183)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:155)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:175)
    at org.apache.http.impl.io.ChunkedInputStream.exhaustInputStream(ChunkedInputStream.java:289)
    at org.apache.http.impl.io.ChunkedInputStream.close(ChunkedInputStream.java:262)
    at org.apache.http.entity.BasicHttpEntity.consumeContent(BasicHttpEntity.java:142)
    at org.apache.http.conn.BasicManagedEntity.consumeContent(BasicManagedEntity.java:114)
    at anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper.Release(HttpClientWrapper.java:531)
    at anywheresoftware.b4a.samples.httputils2.httputils2service._hc_responseerror(httputils2service.java:120)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    ... 10 more
#1 setTransGradationModeColor false
** Activity (main) Resume **
 
Upvote 0

boastrike

Member
Licensed User
Longtime User
There is a message coming back from the server (not found = 404) and Job.Success is false



B4X:
If Job.Success = True Then
    ...
Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
      Msgbox(Job.jobname & ": " & Job.ErrorMessage,"Error")

   End If
   Job.Release

In "else" = Job fails you can check it and decide what to do. Parse "Job.ErrorMessage" for 404.

There is no need to try&catch the send of a job (this works anyway).

A missing image sounds like a logic problem.

Thanks very much for your response. We appreciate your time and consideration. However, I do not think what you outlined is the problem.
The code actually accounts for this. However, it is a large app and I do not wish to post all the code here. As an example, here is the code that catches the issue.

B4X:
Else
        If Job.JobName <> "jobDownloadImage" Then
           Log("Error: " & Job.ErrorMessage)
           ToastMessageShow("Error: " & Job.ErrorMessage, True)
            Msgbox("Network connection error, please retry","Network Connection Issue")
            ProgressDialogHide
            ShowLoginPanel
        Else
            Dim rsie As RSImageEffects
            Dim adHoc As Bitmap
            adHoc.Initialize(File.DirAssets,"no_image.jpg")
            PanelQtyImage.Bitmap = rsie.RoundCorner(adHoc, 20)
        End If
    End If

Also it should be noted, when debugging, if a break point is set at the first if statement of JobDone, the if statement in the original post. The break point is never reached; the app is breaking before JobDone is even called.
 
Upvote 0

boastrike

Member
Licensed User
Longtime User
the httputils2service crashes before firing Jobdone


B4X:
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
Create Finished
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
Failed to read error message.
java.lang.RuntimeException: android.os.NetworkOnMainThreadException
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA$3.run(BA.java:332)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5752)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
    at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:739)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
    at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:220)
    at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:183)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:155)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:175)
    at org.apache.http.impl.io.ChunkedInputStream.exhaustInputStream(ChunkedInputStream.java:289)
    at org.apache.http.impl.io.ChunkedInputStream.close(ChunkedInputStream.java:262)
    at org.apache.http.entity.BasicHttpEntity.consumeContent(BasicHttpEntity.java:142)
    at org.apache.http.conn.BasicManagedEntity.consumeContent(BasicManagedEntity.java:114)
    at anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper.Release(HttpClientWrapper.java:531)
    at anywheresoftware.b4a.samples.httputils2.httputils2service._hc_responseerror(httputils2service.java:120)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    ... 10 more
#1 setTransGradationModeColor false
** Activity (main) Resume **


You are absolutely correct. Thanks for your time and consideration!
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
My Server responds like that:

B4X:
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
GC_CONCURRENT freed 394K, 12% free 7332K/8263K, paused 4ms+1ms, total 25ms
JobName = Ping, Success = true
Ready

Loading Image: http://192.168.178.21/image/uploads/20140822_xxxx.jpg
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<title>Object not found!</title>

<link rev="made" href="mailto:postmaster@localhost" />

<style type="text/css"><!--/*--><![CDATA[/*><!--*/

    body { color: #000000; background-color: #FFFFFF; }

    a:link { color: #0000CC; }

    p, address {margin-left: 3em;}

    span {font-size: smaller;}

/*]]>*/--></style>

</head>



<body>

<h1>Object not found!</h1>

<p>





    The requested URL was not found on this server.



 



    If you entered the URL manually please check your

    spelling and try again.



 



</p>

<p>

If you think this is a server error, please contact

the <a href="mailto:postmaster@localhost">webmaster</a>.



</p>



<h2>Error 404</h2>

<address>

  <a href="/">192.168.178.21</a><br />

  <span>Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3</span>

</address>

</body>

</html>

It seems that your server terminates the connection (I clicked the link). It does not respnd with "not found". Maybe a settings issue?
 
Upvote 0

boastrike

Member
Licensed User
Longtime User
My Server responds like that:

B4X:
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
GC_CONCURRENT freed 394K, 12% free 7332K/8263K, paused 4ms+1ms, total 25ms
JobName = Ping, Success = true
Ready

Loading Image: http://192.168.178.21/image/uploads/20140822_xxxx.jpg
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<title>Object not found!</title>

<link rev="made" href="mailto:postmaster@localhost" />

<style type="text/css"><!--/*--><![CDATA[/*><!--*/

    body { color: #000000; background-color: #FFFFFF; }

    a:link { color: #0000CC; }

    p, address {margin-left: 3em;}

    span {font-size: smaller;}

/*]]>*/--></style>

</head>



<body>

<h1>Object not found!</h1>

<p>





    The requested URL was not found on this server.







    If you entered the URL manually please check your

    spelling and try again.







</p>

<p>

If you think this is a server error, please contact

the <a href="mailto:postmaster@localhost">webmaster</a>.



</p>



<h2>Error 404</h2>

<address>

  <a href="/">192.168.178.21</a><br />

  <span>Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3</span>

</address>

</body>

</html>

It seems that your server terminates the connection (I clicked the link). It does not respnd with "not found". Maybe a settings issue?

This is a clients server. You are right in that a 404 is not being returned. There is an ERR_CONNECTION_RESET being thrown by chrome, which is a way of saying something went wrong (no details). I will suggest the client review their server configuration.

I am hoping b4a can handle this kind of situation. Possible update to HttpUtils.

Thanks very much for your response! Your user image makes me crave a beer!
 
Last edited:
Upvote 0

KZero

Active Member
Licensed User
Longtime User
this may be temporary solution

use HTTP library

B4X:
Sub Process_Globals
Dim hc As HttpClient
End Sub

Sub Activity_Create(FirstTime As Boolean)  
    If FirstTime=True Then hc.Initialize("hc")  
End Sub

Sub CheckURL(aURL As String)
Dim Req As HttpRequest
    Req.InitializeHead(aURL)
    hc.Execute(Req, 1)
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Log("Link is ok")
'now call httputils2 to download the image
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Link not valid!")
End Sub
 
Upvote 0

boastrike

Member
Licensed User
Longtime User
this may be temporary solution

use HTTP library

B4X:
Sub Process_Globals
Dim hc As HttpClient
End Sub

Sub Activity_Create(FirstTime As Boolean) 
    If FirstTime=True Then hc.Initialize("hc") 
End Sub

Sub CheckURL(aURL As String)
Dim Req As HttpRequest
    Req.InitializeHead(aURL)
    hc.Execute(Req, 1)
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Log("Link is ok")
'now call httputils2 to download the image
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Link not valid!")
End Sub

Works like a charm as a temp fix! Great response KZero, thanks so much for your help in getting a solution this issue. You Rock!
 
Upvote 0
Top