Android Question How to flag 404 "page not found" ? [solved]

trepdas

Active Member
Licensed User
Hello good people,

I need to get a boolean true or false for page 404 (page not found) . (such as http://nonexistingdomain.com/342.html)

what would be the best method to do this?

I am trying to use the example here with no success :

when I call the check_http sub and go step by step in debugger mode, it seems that it skips the Sub JobDone completely.

B4X:
Sub check_http
    
    Dim job1 As HttpJob
    job1.Initialize("Job1", Me)
'    'Send a GET request
    job1.Download(WebAddress)
End Sub
'
Sub JobDone (job As HttpJob)
    Log("JobName = " & job.JobName & ", Success = " & job.Success)
    If job.Success = False Then
        Log("Error: " & job.ErrorMessage)
        'do something else
        Msgbox ("error.","")
    
    Else
        Log(job.GetString)
        'do something if ok
    
        WebView1.LoadUrl(WebAddress)
    End If
    job.Release
End Sub





help as usual will be very appreciated.
 

trepdas

Active Member
Licensed User
Thank you so much for the quick reply, mc73

I tried the simple example shown here :

it is not working. from a reason while debugging it skips the wait for and continue executing code


B4X:
Sub check_http
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(WebAddress)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release


End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Sure it's skipped? Try this one:
B4X:
Sub check_http
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(WebAddress)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    else
       log(j.errorMessage):' your error should be here
   End If
   j.Release
End Sub
 
Upvote 0

trepdas

Active Member
Licensed User
yes, I just copied it from you as is. it still skips. it gets to wait for and skips the if j.success (exiting sub)
 
Upvote 0

trepdas

Active Member
Licensed User
attaching, including the uncomment block from previous try...

B4X:
Sub check_http
    
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(WebAddress)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    Else
       Log(j.errorMessage):' your error should be here
   End If
    j.Release
  
    '    Dim job1 As HttpJob
'    job1.Initialize("Job1", Me)
    ''    'Send a GET request
'    job1.Download(WebAddress)
'    Wait For (job1) JobDone(job1 As HttpJob)

    'Sub JobDone (job As HttpJob)
'    Log("JobName = " & job.JobName & ", Success = " & job.Success)
'    If job.Success = False Then
'        Log("Error: " & job.ErrorMessage)
'        'do something else
'        Msgbox ("error.","")
    '
'    Else
'        Log(job.GetString)
'        'do something if ok
    '
'        WebView1.LoadUrl(WebAddress)
'    End If
'    job.Release
    'End Sub
  
  
  
End Sub
 
Upvote 0

trepdas

Active Member
Licensed User
Webaddress is set as public string in the code.
and contain a url that is deliberately inaccessible (404)

:(
Untitled-1.jpg
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Are you calling check_http from inside activity_create? Perhaps you can upload a small project showing the problem which users here can test.
 
Upvote 0

trepdas

Active Member
Licensed User
err.no.

I call it from a sub that is triggered from a button click.

I will need this to execute upon user's click (url availability check)





Untitled-1.jpg
 
Last edited:
Upvote 0

trepdas

Active Member
Licensed User
ok.
it was easy to understand > no webview.
I deleted the webview component.

as for the problem - it persist.

I need this action when a text box is filled by the user and a button is clicked.
the user's string is being converted to a url and then I need to check if 404.


so from the button_click I call a new sub (go) and that sub calls the check_http.(as shown above in the picture)
but it won't work.

it goes until the "wait for" and then skips on to execute the above sub.

what am I missing?
🙏
 
Upvote 0

trepdas

Active Member
Licensed User
I am trying the exact example from the resumable subs video (video time : 24:35)
I want to continue executing code only after the check for the 404 is done. but still with no success. 😫😩

once it gets to the wait for (inside the check sub) - it goes somewhere without getting to the if statement.
I do get the message "*** Service (httputils2service) Create ***" in the log.

am I that stupid? (probably)
should I quit programming? (no)

can anyone post a simple and fast code to check 404 ??



(Webaddress is public string and already contain a url)

B4X:
Sub Go


    CheckInternal
    ReplaceSpaces
    CreateIDN

    Wait For (CheckHttp(WebAddress)) complete (Result As Boolean)
    ' is it 404 ??


    If LN<3 Then
        InternalFeatures
    End If

End Sub




B4X:
Sub CheckHttp (url As String) As ResumableSub
    Dim job As HttpJob
    job.Initialize("", Me)
    job.Download(url)
    Wait For (job) JobDone(job As HttpJob)

    If job.Success Then
        Log(job.GetString)
    Else
       Log(job.errorMessage):' your error should be here
   End If
    job.Release
      Return job.Success
End Sub
 
Last edited:
Upvote 0

trepdas

Active Member
Licensed User
Thank you. I still can't make it work :((((

once it gets to line 'Wait For (job) JobDone(job As HttpJob)'
I get in the log a message "*** Service (httputils2service) Create ***"
but...nothing happens.
the log in sub 'Go' (line 5) is not being executed :(





B4X:
Sub Go

    Wait For (CheckHttp(WebAddress)) complete (flag As Boolean)
    ' is it 404 ??
    Log ("why this line is not being executed?")
   

    If LN<3 Then
        InternalFeatures
    End If
   
End Sub

B4X:
Sub CheckHttp (url As String) As ResumableSub  
    Dim job As HttpJob
    job.Initialize("", Me)
    job.Download(url)
    Wait For (job) JobDone(job As HttpJob)

If job.Success Then
        Log(job.GetString)
    Else
       Log(job.errorMessage):' your error should be here
   End If
    job.Release
      Return job.Success
End Sub
 
Upvote 0
Top