Iorek
Iorek

Reputation: 581

Calling a javascript function does not accept a variable parameter

The following code is designed within a Fragment to call a javascript file (MyMap) and call a function (updateJSONandMap). The function itself sends a JSON to a server and takes three parameter. It works while the first parameter is hardcoded ("Incident") but will not work when I create a variable.

            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setDomStorageEnabled(true);
            webview.loadUrl("file:///android_asset/MyMap.html");
            webview.setWebViewClient(new WebViewClient() {
                public void onPageFinished(WebView view, String url) {
                    webview.loadUrl("javascript:updateJSONandMap('Incident'," + latitude + "," + longitude + ")");
                }
            });

The block of text of the JSON is a string

            Event: {
                Action: 'PUT',
                Value: {"S": UserEvent}
            },

The obvious create a variable

final String incident = "incident2";

and then

webview.loadUrl("javascript:updateJSONandMap(" + incident + "," + latitude + "," + longitude + ")");

does not create an error, but does not send the JSON to the server

Upvotes: 0

Views: 88

Answers (1)

ChickenFeet
ChickenFeet

Reputation: 2813

Edit: I think there may be an error in your string, the first incident is surrounded by single quotations. Will this string work?

"javascript:updateJSONandMap('" + incident + "'," + latitude + "," + longitude + ")";

What if you do it this way around?

final String incident = "incident2";
String loadUrlParam = `javascript:updateJSONandMap('${incident}', ${latitude}, ${longitude})`;

webview.loadUrl(loadUrlParam);

Upvotes: 1

Related Questions