安卓系统,使用javascript接口从webview中提取javascript变量
Android, extract javascript variable from webview using javascript interface
我如何将下面的变量从网站提取到我的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
}
}
相关文章:
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- Android Webview通过Javascript注入CSS
- Android在webview中加载带有javascript的html文件,得到Uncaught ReferenceEr
- 在Android WebView中通过javascript检测点击HTML选项标记
- 用JavaScript替换Android WebView模板上的html文本
- Javascript inject CSS into Swift WebView
- 在Android WebView中运行Javascript-onPageFinished循环
- Chrome 50的Android WebView在触摸页面之前会阻止Javascript
- 在Android的Webview中添加Javascript
- 带有javascript的HTML没有加载到android webview中
- 在WebView iOS中评估Javascript
- JavaFX+WebView/Javascript:setTimeOut不起作用调用堆栈来自Java
- 检查登录错误Android WebView Javascript
- 使用WebView中的javascript触发setVisibility属性;不起作用
- 在Android中执行javascript而不使用webview
- 无法在 android 中运行 JavaScript for webview
- 在 Objective-C 中接收来自 JavaScript WebView 的消息
- 无法在Android中使用Javascript webview获取GPS坐标
- onPageFinished Javascript WebView
- Android Javascript WebView