Android的,从web视图使用JavaScript界面​​提取JavaScript变量 [英] Android, extract javascript variable from webview using javascript interface

查看:161
本文介绍了Android的,从web视图使用JavaScript界面​​提取JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能提取从网站这个变量娄我的Andr​​oid code? 我想它应该使用JavaScript接口,但我怎么得到它呢?

How can I extract this variable bellow from a website to my android code? I guess it should work using javascript interface but how do I get it?

<script type="text/javascript">
    var Ids = "[4161, 104, 121, 202, 1462]";
</script>

和我不能在网站上修改code到返回值的方法。

And I can't change the code on the website to a method that returns the value.

有什么建议?

推荐答案

您可以使用的javascript:在webview.loadurl通话方案。这将在web视图页面执行JavaScript。

You can use the javascript: scheme in a webview.loadurl call. It will execute the javascript in the webview page.

从那里,你可以把它调用一个函数在你的JavaScript接口。

From there you can make it call a function in your javascript interface.

webview.loadUrl("javascript:Android.getIds(Ids);");

机器人是用来定义您的JavaScript接口的名字空间。

Android being the name space used to declare your javascript interface.

//Add the javascript interface to your web view
this.addJavascriptInterface(new CustomJavaScriptInterface(webViewContext), "Android");

要注意的是javascriptinterface只有原始类型的工作。所以,你其实并不能直接传递数组。只需通过你的阵列中使用的JavaScript的方案循环。我看它是不是一个真正的数组,所以你应该罚款只有:

Beware that javascriptinterface only work with primitive types. So you actually can't pass directly an array. Just use the javascript scheme to loop through your array. I see it is not really an array so you should be fine with just :

public class CustomJavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    CustomJavaScriptInterface(Context c) {
        mContext = c;
    }


    /** retrieve the ids */
    public void getIds(final String myIds) {

        //Do somethings with the Ids

}

这篇关于Android的,从web视图使用JavaScript界面​​提取JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆