Android- 使用 addJavaScriptInterface 从 Javascript 返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5314492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:45:41  来源:igfitidea点击:

Android- using addJavaScriptInterface to return a value from Javascript

javascriptandroidwebview

提问by aamiri

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:

所以我知道还有其他帖子解决了同样的问题,我想我已经按照他们的要求写了信,但可惜无济于事。我正在尝试学习如何让我的应用程序与 javascript 交互。我只是希望能够从我的 javascript 返回一个值到我的活动。
这是我的活动:

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:

这是我的 WebView 加载的 html 文件,其中包含我要运行的 javascript:

    <!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?

这不起作用。当我使用“Log.d("YO!", mString); 运行代码时 取消注释我得到一个空指针异常,这意味着 javascript 从未将值分配给 mString。我究竟做错了什么?

采纳答案by Quintin Robinson

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

这可能不正确,我将不得不仔细检查,但这可能是由于参考丢失了..

Try making your ClsAccessorreference a member level one..

尝试使您的ClsAccessor参考成为会员一级..

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?

此外,如果您逐行调试是否setValue()曾经在托管代码中被调用?

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.

值得一提的是,我有我上面描述的代码,并且托管接口的脚本工作正常,但是我目前无法测试并查看没有对类的引用是否也有效。

回答by Anonymous

Try enabling javascript on your webview :

尝试在您的 webview 上启用 javascript:

mWebview.getSettings().setJavaScriptEnabled(true);

回答by bMon

I do it by utilizing the onLoadResource in my Webview class.

我通过在我的 Webview 类中使用 onLoadResource 来做到这一点。

Html page:

html页面:

<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:

然后在我的 onLoadResource 中我监听那个 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.

这可能不是您所需要的,但有一些原因我不得不这样做,所以我认为它可以帮助您。还要注意,我没有做表单提交,我确实<a>提交了,那是因为 onLoadResource 不会选择表单提交。

Edit: This might help.

编辑: 这可能会有所帮助。