在 Android Studio 项目中使用 htmlunit 时,没有 Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier 类型的静态字段 INSTANCE

Deb*_*Ann 8 java android htmlunit android-studio

我在 Android Studio 项目中使用 htmlunit 2.36.0。我成功编译了 apk,但当我尝试获取网页时遇到一些运行时错误。之前,我收到以下错误:

java.lang.BootstrapMethodError: Exception from call site
Run Code Online (Sandbox Code Playgroud)

但我可以通过在 gradle 中添加以下内容来解决这个问题:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
Run Code Online (Sandbox Code Playgroud)

但是,现在我面临另一个错误:

 java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar!classes3.dex)
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:151)
        at com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.buildSSLSocketFactory(HtmlUnitSSLConnectionSocketFactory.java:89)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.configureHttpsScheme(HttpWebConnection.java:635)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClientBuilder(HttpWebConnection.java:558)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClientBuilder(HttpWebConnection.java:519)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:171)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1407)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1326)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:396)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:317)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:469)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:450)
Run Code Online (Sandbox Code Playgroud)

显然,在它自己的类或其超类中没有AllowAllHostnameVerifier的静态字段实例。我不知道如何解决这个问题。

Mr.*_*.AF 0

正如hc.apache.org记录的:

AllowAllHostnameVerifier已弃用。(4.4) 使用NoopHostnameVerifier

如果将依赖项的版本降级到4.3(2013~2015),异常将得到解决。

implementation 'net.sourceforge.htmlunit:htmlunit:2.15'
Run Code Online (Sandbox Code Playgroud)