6

So i know there are other posts addressing the same issue, and I think I've followed them to the letter but alas to no avail. I'm trying to learn how to get my app to interact with javascript. I just want to be able to return a 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 loaded 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 with "Log.d("YO!", mString);" uncommented I get a null pointer exception, which means that the javascript never assigned a value to mString. what am i doing wrong?

aamiri
  • 2,420
  • 4
  • 38
  • 58
  • Are you running into a problem? – Quintin Robinson Mar 15 '11 at 16:11
  • oh silly me. yes i am running into a problem. when the line "Log.d("YO!", mString)" is uncommented I get a NullPointerException, meaning that mString was never assigned a value, which means the binding of javascript to java failed. so what am i doing wrong? – aamiri Mar 15 '11 at 16:28

3 Answers3

3

This may not be correct, I will have to double check but it may be due to the reference being lost..

Try making your ClsAccessor reference a member level one..

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

Also if you debug line by line does setValue() ever get called in managed code?

For what it is worth, I have code that is doing as I have described above and the script to managed interface works fine, however I am not in a position currently to test and see if not having a reference to the class also works.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
2

Try enabling javascript on your webview :

mWebview.getSettings().setJavaScriptEnabled(true);
Anonymous
  • 21
  • 1
0

I do it by utilizing the 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 for that url:

public void onLoadResource(WebView view, String url)
        {
            if(url.contains("http://domain.com/processing.php")){

//process the url and suck out the data

}

This might not be what you need, but there were reasons I had to do it this way so figured it may help you. Also note that I didn't do form submit, I did <a> submit, thats because the onLoadResource won't pick up the form submit.

Edit: This might help.

Community
  • 1
  • 1
bMon
  • 962
  • 4
  • 15
  • 33
  • I m getting void is an invalid type for the variable onLoadResource error when i use this snippet – Rizvan Mar 12 '12 at 13:11
  • Do you have the onLoadResource in your private class YourWebViewClient extends WebViewClient {} ? – bMon Mar 13 '12 at 05:16