0

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

1
  • 1
    obviously incident2 is a variable, 'incident2' is a string Commented Jan 27, 2017 at 1:12

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I just figured this out as I came to type back the answer! Thank you - webview.loadUrl("javascript:updateJSONandMap('" + incident + "'," + latitude + "," + longitude + ")");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.