Я пытаюсь запустить HtmlUnit на Android. Я думаю, этот ответ раньше работал со старыми библиотеками, но не сейчас. Другие темы, связанные с этим вопросом, не касаются Kotlin на Android или устарели.
Рассмотрим код ниже:
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"
}
Androidmanifest:
<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)