0

My application so far records a number of functions that are performed in a web view and stores them into variables. This has been done using JavaScript that I have injected it into my Android Application. So far I have used the JSON stringify function to create a JSONObject as shown.

var myJSONText = JSON.stringify(chars);

My Question is how do I get the myJSONText Variable from the JavaScript to Java so I can fiddle with it there. I have tired to look for how to do this but have not had any luck. Any help or links would be appreciated.

2
  • Your data needs to be submitted to the Java application. This can be done by using Ajax. Commented Jul 10, 2014 at 10:00
  • Can't I use the JavaScriptInterface? I wouldn't know where to start with ajax as I haven't used it before. Commented Jul 10, 2014 at 10:04

1 Answer 1

2

Add a JavascriptInterface to your webview with:

webView.addJavascriptInterface(new MyJavascriptInterface(), "HTMLOUT");
webView.getSettings().setJavaScriptEnabled(true);

Let the JavacriptInterface have a method like:

public void receiveJSON(String json);

Call this method in javascript with:

document.write(window.HTMLOUT.receiveJSON(myJSONText));
Sign up to request clarification or add additional context in comments.

4 Comments

What do I need to put in the java method?
You mean in receiveJSON? Thats where you get your JSON from javascript, so you can start fiddling around as you wished.
LOL. Start fiddling around! 😉
I have tried creating the receive JSON method and put the follwing in. public void receiveJSON(String myJSONText) throws JSONException { JSONObject myObject = new JSONObject(myJSONText); Convert = myObject.toString(); Log.i(TAG, "testinggggg"); } the problem is I cant call the method without it causing an error could could help me with this please.

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.