首页 > 解决方案 > 调用站点 #4 引导方法的异常。代码在 Android Studio 中不起作用,但在 Eclipse 中起作用

问题描述

我正在编写代码以使用 HtmlUnit 登录网站。它一直在 Eclipse 中工作,但现在我决定将它移到 Android Studio 中,在应用程序中使用它。我有 2 个问题。

  1. 为什么 HtmlUnit 需要 API 26,这对于现在的技术来说已经很高了(只有 19% 的用户),但它只是一个 web 模拟器?

  2. 为什么在创建 webClient 时出现此错误?我的例外是:

java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
    at com.gargoylesoftware.htmlunit.WebClient.addDefaultHeaders(WebClient.java:1496)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1392)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1321)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:466)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:448)
    at notas.com.mistarapp.Student.login(Student.java:65)

这是我正在经历的代码。所有内容均已导入,并且未突出显示为错误。这是我的代码:

try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {

    HtmlPage firstPage = webClient.getPage(link);

    // waitForBackgroundJavaScript has to be called after every action
    webClient.waitForBackgroundJavaScript(100);

    System.out.println("Access to the login page is made.");
    System.out.println("-------------------------------------------------------------------------------");

    // Get the form that we are dealing with and within that form,
    // find the submit button and the field that we want to change.
    HtmlForm form = firstPage.getFormByName("loginform");

线上出现错误:

HtmlPage firstPage = webClient.getPage(link);

标签: javaandroidhtmlunit

解决方案


I have fixed this issue by adding Java8 to app-level build.gradle in android section.

Java 8 in your builds to function as of version 9.0.0 and newer. You can learn more about how to enable this at https://developer.android.com/studio/write/java8-support.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}

推荐阅读