1

So I want to go to a html page, find a form, fill it and send it. The problem is that when I do that, I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.metapp, PID: 28270
    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!classes4.dex)
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
        at com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.buildSSLSocketFactory(HtmlUnitSSLConnectionSocketFactory.java:89)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.configureHttpsScheme(HttpWebConnection.java:597)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:532)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClientBuilder(HttpWebConnection.java:494)
        at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:158)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1321)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1238)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:346)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:415)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:400)
        at com.example.metapp.MainActivity.onCreate$lambda-0(MainActivity.kt:41)
        at com.example.metapp.MainActivity.$r8$lambda$nklaWid6AOfxCM7blzcU70CR_Og(Unknown Source:0)
        at com.example.metapp.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:0)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

This is my code:

btnLogin.setOnClickListener {

            val webClient = WebClient()

            try {

                var page = webClient.getPage<Page>("https://mail.metropolitan.ac.rs/?loginOp=logout") as HtmlPage

                val form = page.getFormByName("loginForm")

                val usernameInput = form.getInputByName<HtmlInput>("username")
                val passwordInput = form.getInputByName<HtmlInput>("password")

                usernameInput.valueAttribute = "myUsername"
                passwordInput.valueAttribute = "myPassword"

                page = form.getInputByValue<HtmlInput>("Sign in").click() // works fine
                println(page.asText())

            } catch (e: Exception) {
                e.printStackTrace()

            }
        }

I've added selenium htmlunit driver in my dependencies:

implementation("org.seleniumhq.selenium:selenium-htmlunit-driver:2.52.0")

What am I doing wrong?

Nikola
  • 11
  • 3
  • Android come with a different version of the httpclient - see https://github.com/HtmlUnit/htmlunit/issues/133 for more. – RBRi Oct 12 '21 at 10:56
  • Is selenium-htmlunit the most appropriate library to use for web scraping on Android? – Dale Dec 08 '21 at 19:36
  • After working through some build errors ( https://stackoverflow.com/questions/44342455/more-than-one-file-was-found-with-os-independent-path-meta-inf-license ) and ( https://stackoverflow.com/questions/65987904/android-studio-duplicate-classes-found-in-modules-and ), now I have hit this exact same obstacle. How did you resolve? – Dale Dec 10 '21 at 22:06
  • You may need to update HTMLUnit... AllowAllHostnameVerifier class is obsolete in Android. Try version 2.56 for Selenium 3, or version 3.56 for Selenium 4. https://github.com/SeleniumHQ/htmlunit-driver/releases/tag/3.56.0 – pcalkins Dec 16 '21 at 22:01

0 Answers0