HTMLUnit for Android extremly slow

71 Views Asked by At

Hello i want to build an app with htmlunit for android (Android Studio). If I try to cget the page of youtube this tooks a lot of seconds (over 10). So that is too slow for user experience... other sites are slow too

build.gradle

implementation group: 'org.htmlunit', name: 'htmlunit3-android', version: '3.3.0'

my simple code

   HtmlPage htmlPage;

    try (WebClient webClient = new WebClient()) {

        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        htmlPage = webClient.getPage("https://www.youtube.com/");

    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
2

There are 2 best solutions below

4
Exitron On

Possible duplicate of: HTMLUnit : super slow execution?

According to the accepted answer of the duplicate question, you can disable Javascript for faster execution

webClient.getOptions().setJavaScriptEnabled(false);//if you don't need js

But if you need JavaScript to be enabled then you may try adding a timeout to request

webClient.setJavaScriptTimeout(30000)
3
RBRi On

You can also try to do

webClient.getOptions().setDownloadImages(false);