在Android中执行不带webview的javascript [英] Execute javascript without webview in Android

查看:191
本文介绍了在Android中执行不带webview的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Android应用中执行JS功能。
该函数位于网站上的.js文件中。

I'm trying to execute a JS fonction in my Android app. The function is in a .js file on a website.

我没有使用webview,我想执行JS函数,因为它发送请求我想要。

I'm not using webview, I want to execute the JS function because it sends the request i want.

在我的浏览器的控制台中,我只需要 question.vote(0); ,我怎么能在我的应用程序中执行此操作?

In the Console in my browser i just have to do question.vote(0);, how can I do it in my app ?

推荐答案

更新2018年: AndroidJSCore已被<取代a href =https://github.com/LiquidPlayer/LiquidCore =nofollow noreferrer> LiquidCore ,它基于V8。它不仅包括V8引擎,而且所有Node.js都可用。

UPDATE 2018: AndroidJSCore has been superseded by LiquidCore, which is based on V8. Not only does it include the V8 engine, but all of Node.js is available as well.

您可以在没有WebView的情况下执行JavaScript。您可以使用 AndroidJSCore 。下面是一个快速示例,说明如何执行此操作:

You can execute JavaScript without a WebView. You can use AndroidJSCore. Here is a quick example how you might do it:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://your_website_here/file.js");
HttpResponse response = client.execute(request);
String js = EntityUtils.toString(response.getEntity());

JSContext context = new JSContext();
context.evaluateScript(js);
context.evaluateScript("question.vote(0);");

但是,这很可能不适用于WebView,因为我认为你不仅仅是依赖JavaScript,但AJAX,它不是纯JavaScript的一部分。它需要一个浏览器实现。

However, this most likely won't work outside of a WebView, because I presume you are not only relying on JavaScript, but AJAX, which is not part of pure JavaScript. It requires a browser implementation.

您是否有理由不使用隐藏的WebView并只是注入您的代码?

Is there a reason you don't use a hidden WebView and simply inject your code?

// Create a WebView and load a page that includes your JS file
webView.evaluateJavascript("question.vote(0);", null);     

这篇关于在Android中执行不带webview的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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