Android Question Calling a B4A sub from an external webpage

Richard Rawlins

Member
Licensed User
Hi I have a webpage hosted on a local server which i display via a webview and in that page I want to call a b4a function via javascript. I have been looking at some examples of the webviewextra2 callsub command but i am still having a issue getting it to fire.

Here is my javascript code :
Javascript Code:
function preview(id) {

var ch_url = document.getElementById(id + "_url").value;

B4A.CallSub('play_url', false, 'hello');

}

Here is my B4A code:
B$A code:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

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 webviewextra As WebViewExtras
    Private webviewsettings As WebViewSettings
    Private javascriptinterface As DefaultJavascriptInterface
    
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")
    Activity.LoadLayout("main_menu")
    webviewextra.Initialize(WebView1)
    Dim WebChromeClient1 As DefaultWebChromeClient
    WebChromeClient1.Initialize("WebChromeClient1")
    webviewextra.SetWebChromeClient(WebChromeClient1)
    webviewextra.AddJavascriptInterface(javascriptinterface,"B4A")
    webviewsettings.setCacheMode(WebView1,"LOAD_NO_CACHE")
    WebView1.JavaScriptEnabled = True
    WebView1.ZoomEnabled = False
    WebView1.LoadUrl("http://192.168.10.81/portal/")
    

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    
    Log(KeyCode)
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Log("back pressed")
        WebView1.Back
        Return True
        Else
            Return False
    End If
End Sub

Sub play_url (url As String)
    Log("it worked")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Can anyone see what the issue might be?
 

Richard Rawlins

Member
Licensed User
So I ran a few more test and I can do the reverse, I can call a javascript sub on the external page from with B4A. I am still unable to call a B4A sub from the external javascript.
 
Upvote 0

Richard Rawlins

Member
Licensed User
Hey, yes I tried true as well no luck. I'm not sure if there is a difference in how it's done with webviewextras2 which is what I'm using versus the first version. Also could this be some kind of security issue?
 
Upvote 0

Richard Rawlins

Member
Licensed User
ok below is the last thing I tried so far. Still no luck, I'm not sure what I'm doing wrong.

B4A Code:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

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 webviewextra As WebViewExtras
    Private webviewsettings As WebViewSettings
    
    
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")
    Activity.LoadLayout("main_menu")
    webviewextra.Initialize(WebView1)
    Dim WebChromeClient1 As DefaultWebChromeClient
    WebChromeClient1.Initialize("WebChromeClient1")
    webviewextra.SetWebChromeClient(WebChromeClient1)
    webviewextra.AddJavascriptInterface(WebView1,"B4A")
    webviewsettings.setCacheMode(WebView1,"LOAD_NO_CACHE")
    WebView1.JavaScriptEnabled = True
    webviewextra.JavaScriptEnabled = True
    WebView1.ZoomEnabled = False
    WebView1.LoadUrl("http://192.168.10.81/portal/")
    

End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    
    Log(KeyCode)
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Log("back pressed")
        WebView1.Back
        Return True
        Else
            Return False
    End If
End Sub

Sub play_url (url As String)
    Msgbox("Hello world", "This is the title")
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
    webviewextra.ExecuteJavascript("javascript:preview2()")
End Sub

And here is the javascript code:
Javascript Code:
function preview(id) {

var ch_url = document.getElementById(id + "_url").value;



B4A.CallSub("play_url",true, "it works");
}




function preview2() {
alert("worked");
}

I can call the preview2() function from B4A but still nothing when I try the B4A.callsub from javascript.
 
Upvote 0

rraswisak

Active Member
Licensed User
my last chance, i don't know if this related to webextra version or not, i can call B4A sub in webview function as show here - just like i already suggested, the different is - the page was loaded from folder asset, please check it out in this thread

 
Upvote 0

Richard Rawlins

Member
Licensed User
Hi,
So a few discoveries, I downloaded your demo which used a different version of webviewextra 1.42 vs 2 so once I changed the library I got your demo to work. Now I copied the index file to an external server and could no longer get it to run. It would seem the issue is related to running it externally at this point and not syntax. I'm not sure what to do from here.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
start with uploading a small project which shows the issue
 
Upvote 0

Richard Rawlins

Member
Licensed User
ok sure, we can use rraswisak demo as a test. The reason for having it external for my use case is the webpage I am using is dynamically generated via PHP.

To test please change as follows:

B4X:
WebView1.LoadUrl(WebViewAssetFile("index.html"))

To

B4X:
WebView1.LoadUrl("http://65.166.246.110:8888/portal/test2/index.html")

The files are the same just internal in the app versus external on a server. Thank you guys for trying to help.
 

Attachments

  • webext_sample.zip
    9.8 KB · Views: 263
Upvote 0

Richard Rawlins

Member
Licensed User
Hi Guys,
I got it to work using an older version of B4A on my old laptop. I have to investigate why the new one didn't work but it gave me no signs of errors. The version that is working is 8.30 and the new one is 9.5 beta.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you need to modify the manifest to make http calls. that's why old works, new doesn't

- 28 - Non-ssl (non-https) communication is not permitted by default. It can be enabled in B4A v9+ by adding this line to the manifest editor:
Code:
CreateResourceFromFile(Macro, Core.NetworkClearText)
 
Upvote 0

Richard Rawlins

Member
Licensed User
Hi guys,
I saved the project in the old version of B4A and then loaded it in the new version ran the project and it worked. Either way, it's working now so thanks to everyone for the help. I'm sure I will hit another 3 day road block soon.
 
Upvote 0
Top