webview - detect page not found

exjey

Member
Licensed User
Longtime User
Is there a way of detecting "page not found" error, meaning for some reason a page is not available and to show some msgbox or run another procedure or redirect to another page if it happends. The event that executes when page loads completely is not covering the error case and the URL parameter is as i see the original url from the LoadURL method (?).
 

exjey

Member
Licensed User
Longtime User
Thanks Erel. My real problem is not if the page is available or not ,the problem is that Webview when the page cannot be loaded shows the Url string into its box and that is something i dont want to happend. The Url i am using and brings some user info on screen is supposed to be private :sign0137:

So, i will do what you are suggesting to prevent this.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Take a look at the documentation for my new version of WebViewExtras: WebViewExtras
WebViewClient raises the event ReceivedError (ErrorCode As Int, Description As String, FailingUrl As String).

Here's the documentation for the native android java onReceivedError callback: WebViewClient | Android Developers.
Report an error to the host application. These errors are unrecoverable (i.e. the main resource is unavailable). The errorCode parameter corresponds to one of the ERROR_* constants.

It looks like you can use this event to trap a 404 Page Not Found.

B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim WebView1 As WebView
   Dim WebViewExtras1 As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("")
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   
   WebViewExtras1.Initialize(WebView1)
   
   Dim WebViewClient1 As WebViewClient
   WebViewClient1.Initialize("WebViewClient1")
   
   WebViewExtras1.SetWebViewClient(WebViewClient1)
   
   WebViewExtras1.LoadUrl("http://gooXle.co.uk/")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub WebViewClient1_ReceivedError(ErrorCode As Int, Description As String, FailingUrl As String)
   Log("WebViewClient1_ReceivedError "&ErrorCode)
   '   the WebView will still display 'Web page not available' until we (successfully) load another web page
   WebViewExtras1.LoadUrl("http://google.co.uk/")   '   comment this line and you'll see the 'Web page not available'
End Sub

Tested and working on my Galaxy S3 with Jelly Bean.

Example and library attached.
Note that this version of WebViewExtras is a bit of an alpha and is much untested so not yet properly uploaded to the forum.

Martin.
 

Attachments

  • 20130531 ReceivedError.zip
    6 KB · Views: 1,404
Last edited:
Upvote 0

androb

Member
Licensed User
Longtime User
This is what I have been looking for as well.

I have a problem though - after adding this to the project and replacing the previous webviewextras, I am now getting an error on this function

Error description: Unknown member: executejavascript

Will this functionality be added back in? Or am I getting your library confused with someone elses?


Thank you
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Ah yes...
I completely forgot to add an 'ExecuteJavascript' method to the library!

Any such method is just a convenience method that calls:

B4X:
WebView1.LoadUrl("javascript:"&MyJavascriptStringHere)

It just adds the javascript: pseudo protocol to the start of the javascript statement(s) to be executed and passes the new String to the WebView LoadUrl method.

I'll be adding an ExecuteJavascript method to the new library and re-compiling it at some point but for now can you just do as i described above?

You could even wrap it into a new Sub:

B4X:
Sub ExecuteJavascript (WebView1 As WebView, Javascript As String)
    WebView1.LoadUrl("javascript:"&Javascript)
End Sub

Martin.
 
Upvote 0

androb

Member
Licensed User
Longtime User
Thank you for the quick response.

I do send variables back and forth to the javascript web app - I don't know if this will still be possible with your temporary fix?

Currently, I do this sort of thing... for lots of different actions, e.g passing info back to the app to tell it certain information.

B4X:
MyWebViewExtras.executeJavascript(WebView1,"set_device_id('" &device_id& "')")

For that I need to send parameters collected in the b4a app.



The web app also calls basic4android functions from javascript:


B4X:
function playback_local(filename,file_type)
{
   B4A.CallSub('play_video', true, filename);
   return;
{

Should that still work?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi again.

Yes it will all work as you desire, you just need to modify any B4A code that executes javascript.

B4X:
MyWebViewExtras.executeJavascript(WebView1,"set_device_id('" &device_id& "')")

Becomes:

B4X:
WebView1.LoadUrl("javascript:"&"set_device_id('" &device_id& "')")

Just add javascript: to the start of the String you were previously passing to executeJavascript and pass this new String to the WebView LoadUrl method.

The JavascriptInterface is still there but now a B4A object in it's own right:

B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim WebView1 As WebView
   Dim WebViewExtras1 As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   
   WebViewExtras1.Initialize(WebView1)
   
   Dim JavascriptInterface1 As JavascriptInterface
   JavascriptInterface1.Initialize
   WebViewExtras1.AddJavascriptInterface(JavascriptInterface1, "B4A")
   
   '   a WebViewExtras object extends a WebView object, so a WebViewExtras object has all of the WebView methods and properties plus it's additional methods and properties
   WebViewExtras1.LoadUrl("file:///android_asset/my_web_page.html")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

The WebChromeClient and WebSettings syntax is:

B4X:
   WebViewExtras1.Initialize(WebView1)
   WebSettings1=WebViewExtras1.GetSettings
   '   you can now use WebSettings1 to change the WebView1 WebSettings
   
   '   add a WebChromeClient
   Dim WebChromeClient1 As WebChromeClient
   WebChromeClient1.Initialize("")
   WebViewExtras1.SetWebChromeClient(WebChromeClient1)
   
   Dim JavascriptInterface1 As JavascriptInterface
   JavascriptInterface1.Initialize
   WebViewExtras1.AddJavascriptInterface(JavascriptInterface1, "B4A")

Martin.
 
Upvote 0

enrico

Active Member
Licensed User
Longtime User
Take a look at the documentation for my new version of WebViewExtras: WebViewExtras
WebViewClient raises the event ReceivedError (ErrorCode As Int, Description As String, FailingUrl As String).

Here's the documentation for the native android java onReceivedError callback: WebViewClient | Android Developers.

It looks like you can use this event to trap a 404 Page Not Found.

I can't download this updated version of WebViewExtras from that link.
 
Upvote 0

enrico

Active Member
Licensed User
Longtime User
Thanks. Why I don't have WebViewClient available ? Am I missing a library or something ?

Can I use DefaultWebViewClient or DefaulWebChromeClient ?
 
Last edited:
Upvote 0
Top