有java和javascript交互的麻烦

时间:2021-09-07 00:26:59

I have seen several questions similar to mine, but none seem to quite answer my question.

我看过几个与我类似的问题,但似乎没有一个问题可以解答我的问题。

I have some javascript in my assets folder, and i want to use a WebView (or any other way that works) to bind a java object to it so that my server can send updates to my js, and then have my js call my java object.

我在我的资源文件夹中有一些javascript,我想使用WebView(或任何其他工作方式)将java对象绑定到它,以便我的服务器可以向我的js发送更新,然后让我的js调用我的java宾语。

I have tried loadUrl() and loadDataWithBaseURL() and neither seem to work, I use the getURL() after setting it to check it and it always returns null. After looking at the other questions, I can tell I'm referencing the path correctly (or at least the same as them).

我已经尝试了loadUrl()和loadDataWithBaseURL(),似乎都没有工作,我设置它后使用getURL()来检查它,它总是返回null。在查看其他问题后,我可以告诉我正确引用路径(或者至少与它们相同)。

I should note that it is a .js file, not an html file with js in it. However, I haven't seen anything saying that doesn't work / I shouldn't do that.

我应该注意它是一个.js文件,而不是一个带有js的html文件。但是,我没有看到任何说不起作用/我不应该这样做。

Here is my code to set it:

这是我设置它的代码:

    interacter = (WebView) findViewById(R.id.interact);
    interacter.getSettings().setJavaScriptEnabled(true);
    interacter.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    interacter.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    interacter.setClickable(true);
    interacter.addJavascriptInterface(game, "Android");
    //loadDataWithBaseURL was here
    Log.i("Path is", interacter.getUrl() +" ");

Am I doing something wrong, is there a better way to do it, does this even work?

我做错了什么,有没有更好的方法去做,这甚至有用吗?

EDIT

I noticed used the loadDataWithBaseURL() wrong (the commented line in my code was where it was), and I am looking into that at the moment. Help on this still appreciated

我注意到使用了loadDataWithBaseURL()错误(我的代码中的注释行就是这样),我现在正在研究它。对此的帮助仍然很感激

1 个解决方案

#1


0  

I poked around a little more and found this question

我捅了一下,发现了这个问题

Android webview, loading javascript file in assets folder

Android webview,在assets文件夹中加载javascript文件

And I was able to use it to solve my problem.

我能够用它来解决我的问题。

#1


0  

I poked around a little more and found this question

我捅了一下,发现了这个问题

Android webview, loading javascript file in assets folder

Android webview,在assets文件夹中加载javascript文件

And I was able to use it to solve my problem.

我能够用它来解决我的问题。