android - HtmlUnit Android:NoSuchFieldError:没有 Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier 类型的静态字段实例
问题描述
我正在尝试在 Android 上运行 HtmlUnit。我认为这个答案曾经适用于较旧的库,但不再适用。与此问题相关的其他线程与 Android 上的 Kotlin 无关或已过时。
考虑下面的代码:
https://github.com/JimClermonts/HtmlUnit-for-Android
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
doAsync {
val webClient = WebClient()
var page: HtmlPage = webClient.getPage<HtmlPage>("http://www.google.com")
val searchBox = page.getElementByName<HtmlInput>("q")
searchBox.valueAttribute = "htmlunit"
val googleSearchSubmitButton: HtmlSubmitInput = page.getElementByName("btnG") // sometimes it's "btnK"
page = googleSearchSubmitButton.click()
val resultStatsDiv: HtmlDivision = page.getFirstByXPath("//div[@id='resultStats']")
System.out.println(resultStatsDiv.asText()) // About 309,000 results
webClient.close()
}
}
这是我的项目依赖项:
dependencies {
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
// implementation group: 'commons-logging', name: 'commons-logging', version: '1.2'
implementation group: 'commons-net', name: 'commons-net', version: '3.6'
implementation group: 'net.sourceforge.htmlunit', name: 'htmlunit-core-js', version: '2.34.0'
implementation group: 'net.sourceforge.htmlunit', name: 'htmlunit-cssparser', version: '1.3.0'
implementation group: 'net.sourceforge.htmlunit', name: 'neko-htmlunit', version: '2.34.0'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.6'
implementation group: 'org.eclipse.jetty.websocket', name: 'websocket-client', version: '9.4.15.v20190215'
implementation group: 'xalan', name: 'xalan', version: '2.7.2'
//Project Transitive Dependencies
implementation('org.apache.httpcomponents:httpmime:4.5.7') {
exclude module: 'httpclient'
}
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
// implementation group: 'commons-codec', name: 'commons-codec', version: '1.11'
// implementation('org.apache.httpcomponents:httpmime:4.5.7') {
// exclude module: 'httpclient'
// }
// implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
//// implementation group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.11'
// implementation group: 'org.eclipse.jetty', name: 'jetty-client', version: '9.4.15.v20190215'
// implementation group: 'org.eclipse.jetty', name: 'jetty-http', version: '9.4.15.v20190215'
// implementation group: 'org.eclipse.jetty', name: 'jetty-io', version: '9.4.15.v20190215'
// implementation group: 'org.eclipse.jetty', name: 'jetty-util', version: '9.4.15.v20190215'
// implementation group: 'org.eclipse.jetty', name: 'jetty-xml', version: '9.4.15.v20190215'
// implementation group: 'org.eclipse.jetty.websocket', name: 'websocket-api', version: '9.4.15.v20190215'
// implementation group: 'org.eclipse.jetty.websocket', name: 'websocket-common', version: '9.4.15.v20190215'
// implementation group: 'xalan', name: 'serializer', version: '2.7.2'
// implementation group: 'xerces', name: 'xercesImpl', version: '2.12.0'
// implementation group: 'xml-apis', name: 'xml-apis', version: '1.4.01'
implementation "org.jetbrains.anko:anko-commons:0.10.1"
implementation "org.jetbrains.anko:anko-design:0.10.1"
implementation "org.jetbrains.anko:anko-coroutines:0.10.1"
}
安卓清单:
<uses-permission android:name="android.permission.INTERNET" />
错误信息:
W/System.err: 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)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:149)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.buildSSLSocketFactory(HtmlUnitSSLConnectionSocketFactory.java:89)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.HttpWebConnection.configureHttpsScheme(HttpWebConnection.java:626)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:558)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClientBuilder(HttpWebConnection.java:519)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:171)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1402)
2019-04-12 09:17:06.712 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1321)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:466)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:448)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at eu.theappfactory.myapplication.MainActivity$onCreate$1.invoke(MainActivity.kt:20)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at eu.theappfactory.myapplication.MainActivity$onCreate$1.invoke(MainActivity.kt:12)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Async.kt:140)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at org.jetbrains.anko.AsyncKt$doAsync$1.invoke(Unknown Source:0)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at org.jetbrains.anko.AsyncKt$sam$Callable$761a5578.call(Unknown Source:2)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
2019-04-12 09:17:06.713 5110-5140/eu.theappfactory.myapplication W/System.err: at java.lang.Thread.run(Thread.java:764)
解决方案
从版本 2.52.0 开始,依赖于 java.awt 的类的数量现在要少得多。如果您喜欢其余的解决方案,请在 github 上打开一个问题,我将尝试为剩余的依赖项找到一种方法。
推荐阅读
- node.js - npm 没有安装在正确的目录中
- xcode - 如何修复错误“您无权写入文件“_malloc.h”所在的文件夹。” 在 Xcode 中?
- c++ - 使用 >= 或值 - 1?
- nsis - NSIS 安装程序:错误:名为“INSTALL_LIB_INET”的函数已存在
- mysql - 如果一行中满足条件,如何从查询中排除多个结果
- django - 为两种不同的用户类型(客户、供应商)扩展抽象用户模型
- r - 调整以便正确生成表格
- reactjs - 空检查后对象可能为“空”
- reactjs - 使用 react router dom v6 保护路由 渲染没有返回任何内容
- c++ - 如何从我的函数中获取 TSP 导览?