Android - using addJavaScriptInterface to return a value from Javascript

So, I know that there are other messages dedicated to the same problem, and I think that I followed them in the letter, but, alas, to no avail. I am trying to learn how to get my application to interact with javascript. I just want to return the value from my javascript to my activity.
here is my activity:

public class JSExample extends Activity { WebView mWebView; String mString; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView)findViewById(R.id.mWebView); mWebView.addJavascriptInterface(new ClsAccessor(), "accessor"); String html = getAssetsContent("jsinterface.html"); mWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null); // Log.d("YO!", mString); } private String getAssetsContent(String filename){ ..... } private void closeStream(BufferedReader stream) { ..... } class ClsAccessor{ public void setValue(String value){ JSExample.this.mString = value; } } 

Here is the html file uploaded by my WebView that contains the javascript I want to run:

  <!DOCTYPE html> <html> <head> <script language="javascript"> accessor.setValue('Hello!'); </script> </head> <body> <h1>Testing</h1> </body> </html> 

This does not work. When I run the code using "Log.d" ("YO!", MString); uncommented I get a null pointer exception, which means that javascript never assigned the value mString. what am I doing wrong?

+6
javascript android webview
source share
3 answers

This may not be correct, I will have to double check, but it may be due to the loss of the link.

Try making the ClsAccessor link a level one member.

 public class JSExample extends Activity { ... ClsAccessor _accessor = new ClsAccessor(); ... public void onCreate(Bundle savedInstanceState) { ... mWebView.addJavascriptInterface(_accessor, "accessor"); ... 

Also, if you are debugging line by line, is setValue() ever called in managed code?

For what it's worth, I have code that runs as I described above, and the managed script interface works fine, however, I am not able to check it and see if the class reference also works.

+3
source share

Try to enable javascript on webview:

 mWebview.getSettings().setJavaScriptEnabled(true); 
+2
source share

I do this using onLoadResource in my Webview class.

HTML page:

 <html> <head> function Submit(){ var username = $('#username').val(); var urlNew = 'processing.php'; urlNew = urlNew + '?' + 'username=' + username; $('#submit').attr('href', urlNew); return true; } </head> <body> <form> <input id="username" name="username" type="text" /> <a href="" id="submit" onclick="return Submit();" ><img src="images/button.png" /></a> </form> </body> </html> 

So then in my onLoadResource I listen to this url:

 public void onLoadResource(WebView view, String url) { if(url.contains("http://domain.com/processing.php")){ //process the url and suck out the data } 

Perhaps this is not what you need, but there were reasons why I should have done it in such a way as to understand that it could help you. Also note that I did not submit submit, I did <a> submit because onLoadResource will not receive the submit form.

Edit: This may help.

0
source share

All Articles