我如何将下面的变量从网站提取到我的android代码中?我想它应该使用javascript接口工作,但我该如何获得它?
<script type="text/javascript">
var Ids = "[4161, 104, 121, 202, 1462]";
</script>
我无法将网站上的代码更改为返回值的方法。
有什么建议吗?
您可以在webview.loadurl调用中使用javascript:
方案。它将在webview页面中执行javascript。
从那里,您可以让它调用javascript界面中的一个函数。
webview.loadUrl("javascript:Android.getIds(Ids);");
Android是用于声明javascript接口的名称空间。
//Add the javascript interface to your web view
this.addJavascriptInterface(new CustomJavaScriptInterface(webViewContext), "Android");
请注意,javascriptinterface仅适用于基元类型。所以你实际上不能直接传递一个数组。只需使用javascript方案在数组中循环即可。我看到它并不是一个真正的数组,所以你应该只需要:就可以了
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
}
}