简体   繁体   English

在 Java (Android) 中运行 javascript 文件

[英]Running a javascript file within Java (Android)

I have a website that I want to log into from an android app and in order to submit the POST request with right information, they need some hashed values.我有一个网站,我想从一个 android 应用程序登录,为了提交带有正确信息的 POST 请求,他们需要一些散列值。 The way that the website does it is through a javascript file.该网站的工作方式是通过 javascript 文件。 I tried tracing through it but I couldn't get the same result as the native website.我尝试通过它进行跟踪,但无法获得与本机网站相同的结果。

So i've decided to download their javascript file and run it within java.所以我决定下载他们的javascript文件并在java中运行它。

My issue is that I can't figure out how to run specific methods within their file.我的问题是我不知道如何在他们的文件中运行特定的方法。

Here's my code so far and it's extremely bare bones.到目前为止,这是我的代码,它非常简单。

public void hashIt(String valueForMethod1, String valueForMethod2){
    final ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine engine = sem.getEngineByName("JavaScript");
    try {
        engine.eval(new java.io.FileReader("res/raw/theirFile.js"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

Any help would be much appreciated :)任何帮助将非常感激 :)

Thanks谢谢

You can use Invocable to invoke a method of the Javascript that has been loaded您可以使用Invocable来调用已加载的 Javascript 的方法

Invocable inv = (Invocable) engine;
inv.invokeFunction("foo", "bar"); // invoke foo("bar")

See this page for a deeper introduction请参阅此页面以获取更深入的介绍

Edit编辑

As stated by Morrison Chang, apparently there is no javax.script namespace on Android, so you may check the thread he gave a link to in its comment.正如 Morrison Chang 所说,Android 上显然没有javax.script命名空间,因此您可以查看他在评论中提供链接的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM