Android Question When does a website become active?

MarkBnz14

New Member
The site (example) www.xxxxxxxxx.com is duly registered with the NIC (Network Information Center) but has not yet published anything except the following page :
<html><head></head><body></body> </html>
However I don't know the name of this page (www.xxxxxxxxx.com/index.htm or www.xxxxxxxxx.com/index.html or www.xxxxxxxxx.com/default.html or .... I don't know). I cannot download it with a Job.
I would like to be notified with a long loud ring when this site posts something.
I quickly created an app that every X seconds loads the site mentioned in a webview and then examines the content of the webview itself.
If the length of the loaded page exceeds 39 bytes it should mean that the site has published a significant page and therefore the alarm should go off.
However, it happens that after about ten minutes a page appears indicating that this page is not available with a message: ERR_TIMED_OUT.
The length of this HTML page is 2278 bytes.
It would seem logical to think that the site actually posts something meaningful when my app reports a length> 2278 bytes (or maybe different from both 39 and 2278).
I obviously don't have the chance to test this app too much so I ask if the approach I invented is valid or if there is a better one.
This is the code (synthesized) I used.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    URL1="www.xxxxxxxxxxxxx.com"
    Timer1.Initialize("Timer1",300000)
    WebView1.Initialize("WB1")
    Activity.AddView(WebView1, 0,250dip,100%x,100%y-250dip)
    WebView1.JavaScriptEnabled = True
    WebViewExtras1.Initialize(WebView1)
    WebViewExtras1.JavaScriptEnabled = True
    JS.Initialize
    WebViewExtras1.AddJavascriptInterface(JS, "B4A")
end sub

Sub WB1_PageFinished(URL As String)
    ToastMessageShow("Load " & URL ,False  )
    Dim script As String
    script = "B4A.CallSub('ProcessHTML', true, document.documentElement.outerHTML)"
    WebViewExtras1.ExecuteJavascript(script)
End Sub

Sub LoadPageX
    '--- delete cache
    Dim o As Reflector
    o.Target = WebView1
    o.RunMethod2("clearCache","True","java.lang.boolean")
    WebView1.LoadUrl(URL1)
    Sleep(0)
End Sub

Sub Timer1_tick
    LoadPageX   
End Sub

Sub ProcessHTML(Html As String)
   '   This is the Sub that we'll get the web page to send it's HTML content to
   '   Log may truncate a large page so you'll not see all of the HTML in the log but the 'html' String should still contain all of the web page HTML
   Dim sf        As StringFunctions
   Dim L        As Long
   sf.Initialize   
   LogColor(Html,Colors.blue )
   L=sf.Len(Html)
   LogColor("L=" & L,Colors.Blue) 
   If L>2300 Then
       '  VERY LONG BEEP
   End If     
End Sub
 

emexes

Expert
Licensed User
Sounds good except that, instead of relying on the page length - which might change if it contains a date or time - perhaps disregard pages if they contain specific strings that only appear in pages you're NOT interested in eg "<body></body>" and "blah blah blah ERR_TIMED_OUT blah blah blah".

So your code might look something like:
B4X:
Do
    Dim WebPage As String = GetWebPage("www.xxxxxx.com/index.html")

    If WebPage.IndexOf("<body></body>") > 0 Then
       rem ignore empty page
    ElseIf WebPage.IndexOf("<b>ERR_TIMED_OUT<b>") > 0 Then    'or some other identifying string that only appears on timeout webpage
       rem ignore timeout error page
    Else
        Klaxons.Activate(Loud)
    End If

    Sleep(X * 1000)
Loop
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
The suggestion is very valid.
In my case I have made some small changes as I am not able to know if the page I am about to load is really index.html.
B4X:
Sub ProcessHTML(Html As String)
   LogColor(Html,Colors.Blue)
    If Html.IndexOf("<body></body>") > 0 Then
        Log("rem ignore empty page")
    Else If Html.IndexOf("ERR_TIMED_OUT") > 0 Then  
       Log("rem ignore timeout error page")
    Else
        '  SOUND !!!!!!
    End If
End Sub
 
Last edited:
Upvote 0
Top