Sub Process_Globals
Dim hc As HttpClient
Dim req As HttpRequest
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layoutMain")
' add the B4A javascript namespace to the WebView
'WebInterface.addJSInterface(WebView1, "B4A")
' now load a web page
WebView1.LoadUrl("http://www.b4x.com/")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub WebView1_PageFinished (Url As String)
getremotemessages("http://www.b4x.com/")
End Sub
Sub GetRemoteMessages(postUrl As String)
'Set up an http request connection
req.InitializeGet(PostUrl)
hc.Execute(req, 1)
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int) 'We got connection and data !!
Dim resultString As String
resultString = Response.GetString("UTF8") 'This holds the returned data
WebView1.LoadHtml(resultString)
response.Release
File.WriteList(File.DirDefaultExternal,"trial.html",resultstring)
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int) 'No connection :-(
Log("Error connecting: " & Reason & " " & StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
ProgressDialogHide
Response.Release
End If
End Sub
Here's an alternative that should do as the OP originally asked.
First you need to use my JSInterface library.
Now you can execute javascript in your WebView from your B4A code and your WebView can also now call a B4A Sub.
B4X:'Activity module Sub Process_Globals 'These global variables will be declared once when the application starts. 'These variables can be accessed from all modules. End Sub Sub Globals 'These global variables will be redeclared each time the activity is created. 'These variables can only be accessed from this module. Dim myInterface As JSInterface Dim WebView1 As WebView End Sub Sub Activity_Create(FirstTime As Boolean) Activity.LoadLayout("layoutMain") WebView1.Width=100%x WebView1.Height=100%y ' add the B4A javascript interface to the WebView myInterface.addJSInterface(WebView1, "B4A") ' now load a web page WebView1.LoadUrl("http://www.b4x.com/android/help/jsinterface.html") End Sub Sub Activity_Resume End Sub Sub Activity_Pause (UserClosed As Boolean) End Sub Sub WebView1_PageFinished (Url As String) ' Now that the web page has loaded we can get the page content as a String Dim jsStatement As String jsStatement="B4A.CallSub('processHTML', document.documentElement.outerHTML)" Log("PageFinished: "&jsStatement) myInterface.execJS(WebView1, jsStatement) 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 Log("processHTML: "&html) End Sub
You can see that the code waits for the PageFinished event before requesting that the web page sends it's HTML content to the B4A Sub 'processHTML'.
This method has an advantage over the previously posted examples - that is when a web page had been dynamically created and it's content is not static.
The user may have submitted a form for example and be viewing a (dynamically generated) web page that cannot be requested using HttpClient.
Martin.
WvE(i).addJavascriptInterface(wv(i), "B4A")
jsStatement="B4A.CallSub('processHTML', document.documentElement.outerHTML)"
WvE(i).executeJavascript(wv(i), jsStatement)
CallSub(subName As String, callUIThread As boolean, parameter1 As String)
The callUIThread parameter is an important update - it's not available with JSInterface.
Does the Sub called by your javascript modify your activity UI?
If the answer is yes then you need to pass boolean true as callUIThread otherwise you can pass false.
If you pass false and then the Sub tries to modify your activity UI you will get an exception.
Dim callUIThread As Boolean
callUIThread=??
jsStatement="B4A.CallSub('processHTML', callUIThread, document.documentElement.outerHTML)"
WvE(i).executeJavascript(wv(i), jsStatement)
"B4A.CallSub('processHTML', true, document.documentElement.outerHTML)"
"B4A.CallSub('processHTML', false, document.documentElement.outerHTML)"
MyWebViewExtras.addWebChromeClient(WvE(i))
Sub SaveHTML (i As Int)
' Now that the web page has loaded we can get the page content as a String
Dim jsStatement As String
jsStatement="B4A.CallSub('processHTML', false, document.documentElement.outerHTML)"
WvE(i).executeJavascript(wv(i), jsStatement)
End Sub
Sub processHTML(html As String) As String
Log("processHTML: "&html)
Return html
End Sub
Could not find method android.webkit.WebView.canZoomIn, referenced from method thalmy.webviewxtended.xtender.canZoomIn
VFY: unable to resolve virtual method 412: Landroid/webkit/WebView;.canZoomIn ()Z
VFY: replacing opcode 0x6e at 0x0000
VFY: dead code 0x0003-0008 in Lthalmy/webviewxtended/xtender;.canZoomIn (Landroid/webkit/WebViewLjava/lang/Boolean;
Could not find method android.webkit.WebView.canZoomOut, referenced from method thalmy.webviewxtended.xtender.canZoomOut
VFY: unable to resolve virtual method 413: Landroid/webkit/WebView;.canZoomOut ()Z
VFY: replacing opcode 0x6e at 0x0000
VFY: dead code 0x0003-0008 in Lthalmy/webviewxtended/xtender;.canZoomOut (Landroid/webkit/WebViewLjava/lang/Boolean;
JNI WARNING: jarray 0x40580f90 points to non-array object (Ljava/lang/String
"WebViewCoreThread" prio=5 tid=11 NATIVE
| group="main" sCount=0 dsCount=0 obj=0x40583d68 self=0x267ab8
| sysTid=12760 nice=0 sched=0/0 cgrp=default handle=2523448
| schedstat=( 509754101 354504239 113 )
at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:246)
at android.webkit.WebViewCore.loadUrl(WebViewCore.java:1570)
at android.webkit.WebViewCore.access$1400(WebViewCore.java:53)
at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:956)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
at java.lang.Thread.run(Thread.java:1019)
This is a bug with 2.3.x. simulator - Javascript->Java bridge is bugged.
The Google bug specifically uses this demo code. Issue 12987 - android - Javascript to Java Bridge Throws Exception on Android 2.3 - Android - An Open Handset Alliance Project - Google Project Hosting
Go star it so they know to fix it soon.
You can workaround this for now by just using a 2.2 emulator.
Are you running your code on a 2.3 emulator?
Martin.
Dim jsStatement
jsStatement="B4A.CallSub('processHTML',true, document.documentElement.outerHTML)"
MyWebViewExtras.executeJavascript(WebView1,jsStatement)
---------------
Sub processHTML(html As String)
Log("processHTML: " & html)
End Sub
ReferenceError: Can't find variable: B4A in undefined (Line: 1)
MyWebViewExtras.addJavascriptInterface(WebView1, "B4A")
'Activity module
Sub Process_Globals
End Sub
Sub Globals
Dim myInterface As WebViewExtras 'Requires WebViewExtras library which replaced JSInterface Library
Dim WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layoutMain")
myInterface.addJavascriptInterface(WebView1, "B4A")
WebView1.LoadUrl("http://www.b4x.com/android/help/jsinterface.html")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub WebView1_PageFinished (Url As String)
Dim jsStatement As String = "B4A.CallSub('processHTML', document.documentElement.outerHTML)"
myInterface.executeJavascript(WebView1, jsStatement)
End Sub
Sub processHTML(html As String)
Log("processHTML: "&html)
End Sub
I don't have 3.2,but I think it it something to do with obfuscation. See: http://www.b4x.com/android/forum/threads/android-code-obfuscation.13773/
If you are running with the sample code under Run(obfuscation), the Sub processHTML will be lost. You need to change it to process_HTML(something with _) .
Ignoring event: processhtml. Raised from the wrong thread.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?