Android Question WebView Extra Execute JavaString issue

Cableguy

Expert
Licensed User
Longtime User
Hi Guys,

I'm using the WebView along with WebviewExtras in order to navigate into a page, retrieve the html document, and upon that, excute some injection...
The get the Html document part is done, and working like a charm, but when I try to execute another JavaScript to inject a value, I get the following error....

java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {21174b47} called on Looper (JavaBridge, tid 3532) {3c2554c9}, FYI main Looper is Looper (main, tid 1) {21174b47})

I'm lost, I can only guess that I'm calling on the wrong thread, BUT I'm not threading!

Also, what would be the right javascript string to inject a value to a textfield that has no id, only a name, within a form (login form)?How do I execute without callback to B4A?
 

DonManfred

Expert
Licensed User
Longtime User
Make note of the parameter (boolean) which decides to do UI changes or not.
In your case it must be set to true i think
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I tried that, same error!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Guys, PLEASE!!!!

I just can't understand why or what it means!

B4X:
LogCat connected to: 0123456789ABCDEF
--------- beginning of system
--------- beginning of main
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
login page
trying to set the form
main_setuserform (java line: 412)
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {21174b47} called on Looper (JavaBridge, tid 10922) {3c2554c9}, FYI main Looper is Looper (main, tid 1) {21174b47})
    at android.webkit.WebView.checkThread(WebView.java:2345)
    at android.webkit.WebView.evaluateJavascript(WebView.java:1003)
    at uk.co.martinpearman.b4a.webviewextras.WebViewExtras.executeJavascript(WebViewExtras.java:271)
    at b4a.example.main._setuserform(main.java:412)
    at b4a.example.main._process_html(main.java:394)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at uk.co.martinpearman.b4a.webviewextras.WebViewExtras$1B4AJavascriptInterface.CallSub(WebViewExtras.java:73)
    at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
    at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {21174b47} called on Looper (JavaBridge, tid 10922) {3c2554c9}, FYI main Looper is Looper (main, tid 1) {21174b47})
    at android.webkit.WebView.checkThread(WebView.java:2335)
    ... 15 more
** Activity (main) Pause, UserClosed = true **

My code is just a concept idea for the moment and its like I need to draw a car but the pen wont work!

B4X:
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.

    Private WebView1 As WebView
    Private WebSettings As WebViewSettings
    Private WebExtras As WebViewExtras

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    WebExtras.addWebChromeClient(WebView1, "")
    WebSettings.setSaveFormData(WebView1, False)
    WebSettings.setLoadsImagesAutomatically(WebView1,False)
    WebExtras.addJavascriptInterface(WebView1, "B4A")
    WebView1.LoadUrl("http://ts2.travian.pt")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub WebView1_PageFinished (Url As String)

   Dim Javascript As String = $"B4A.CallSub('Process_HTML', false, document.documentElement.outerHTML)"$
   WebExtras.executeJavascript(WebView1, Javascript)
End Sub

private Sub Process_HTML(Html As String)
    Dim Page As String
    Page = Html.SubString2(Html.IndexOf("<body class="), Html.IndexOf2(">", Html.IndexOf("<body class=")))

    If Page.Contains("login") Then
           Log("login page")
        SetUserForm
    else if Page.Contains("village1") Then
        Log("Fields page")
    End If
   
    Log(Page)
End Sub

private Sub SetUserForm
    Log("trying to set the form")
    Dim javascript As String = "document.forms.login.name.value='MyUserNameHere';document.forms.login.password.value='MyPasswordhere';" 'document.forms.myForm.submit();"
      WebExtras.executeJavascript(WebView1, javascript)

End Sub

private Sub Dummy
   
End Sub
 
Upvote 0
Top