17

Has anybody gotten HTMLUnit (or HtmlUnitDriver) to work on Android apps?

This is the problem: I am getting the following error message:

11-26 16:27:26.617: E/AndroidRuntime(1265): java.lang.NoClassDefFoundError: org/w3c/dom/css/CSSRule

This is what I did: I tried adding adding references to the jars listed in the following link (under both Project Dependencies and Project Transitive Dependencies - compile only, excluding test jars):

http://htmlunit.sourceforge.net/dependencies.html

However Eclipse kept crashing, then I found a few questions saying some jars are already contained in the Android SDK:

xalan, xercesImpl and xml-apis

HtmlUnit on Android

HttpClient

Android Programming HtmlUnit

So I removed the references to these jars keeping only the following:

commons-codec-1.4.jar
commons-collections-3.2.1.jar
commons-io-2.0.1.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
cssparser-0.9.5.jar
htmlunit-2.9.jar
htmlunit-core-js-2.9.jar
httpmime-4.1.2.jar
nekohtml-1.9.15.jar
sac-1.3.jar
httpcore-4.1.3.jar

It was at this point when the app was able to run but when the following line of code was executed I started to get the error:

final WebClient webClient = new WebClient();

11-26 16:27:26.617: E/AndroidRuntime(1265): java.lang.NoClassDefFoundError: org/w3c/dom/css/CSSRule

I did a web search and found that xml-apis-1.3.04.jar contains org/w3c/dom/css/CSSRule so I put that reference back to the project but the app wouldn't build at all saying the same error message described in the first link above:

[2011-11-26 16:39:52 - Myproj] Conversion to Dalvik format failed with error 1

Could somebody please shed some light on this?

Community
  • 1
  • 1
Ulises
  • 13,229
  • 5
  • 34
  • 50
  • 1
    According to their site: "It is specifically a way to simulate a browser for testing purposes and is intended to be used within another testing framework such as JUnit or TestNG." I'm wondering why do you need to simulate a virtual browser, when Android has a Webkit browser and JUnit testing? https://developer.android.com/tools/testing/testing_android.html – NoBugs Feb 10 '13 at 02:53

2 Answers2

2

xml-apis-1.3.04.jar contains core classes. You have to modify the jar and delete those classes.

Please note that, at least in current ADT version, you are getting a message explaining the problem:

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
Juan Sánchez
  • 980
  • 2
  • 10
  • 26
0

Just as ULsies stated in the comments Htmlunit does this ==> HtmlUnit is a "GUI-Less browser for Java programs". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc... just like you do in your "normal" browser.

which is exactly similar to webview widget provided inside the android sdk you dont need any other extra jar to open a web page inside your activity

as per google android documentation A WebView is a View that displays web pages.It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.

add this to your activity layout xml file.

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
  />

and this to your activity class file..

WebView myWebView = (WebView) findViewById(R.id.webview);
 myWebView.loadUrl("http://www.your url .com");

more on this here: http://developer.android.com/guide/webapps/webview.html

user1910290
  • 537
  • 4
  • 15
  • 28
  • 2
    Maybe he wants to use it for parsing instead of just viewing the website – user1810737 Aug 09 '13 at 11:00
  • webview gives you the ability to fetch the source code of the website which can be stored and parsed. Many tutorials are there to do this just google it! – user1910290 Aug 11 '13 at 13:56
  • I also need this. I need it in a Android Service to parse an html page after some javascript code ran. So can you help me how to make htmlunit work on Android? – Totalys Feb 15 '14 at 19:33