5

Here is my code:

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

final WebClient webClient = new WebClient();
final HtmlPage startPage = webClient.getPage("http://htmlunit.sf.net");

And this is the error I get:

11-04 21:58:08.761: ERROR/dalvikvm(17868): Could not find class 'com.gargoylesoftware.htmlunit.DefaultCssErrorHandler', referenced from method com.gargoylesoftware.htmlunit.WebClient.<init>
11-04 21:58:08.771: ERROR/dalvikvm(17868): Could not find class 'com.gargoylesoftware.htmlunit.DefaultCssErrorHandler', referenced from method com.gargoylesoftware.htmlunit.WebClient.<init>
11-04 21:58:09.501: ERROR/AndroidRuntime(17868): FATAL EXCEPTION: AsyncTask #1
11-04 21:58:09.501: ERROR/AndroidRuntime(17868): java.lang.RuntimeException: An error occured while executing doInBackground()
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.lang.Thread.run(Thread.java:1027)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868): Caused by: java.lang.NoClassDefFoundError: com.gargoylesoftware.htmlunit.DefaultCssErrorHandler
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:157)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:180)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at com.Testapp4.fa.ForumAdapter.isGood(ForumAdapter.java:209)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at com.Testapp4.BoardIndex$pbar.doInBackground(BoardIndex.java:82)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at com.Testapp4.BoardIndex$pbar.doInBackground(BoardIndex.java:1)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-04 21:58:09.501: ERROR/AndroidRuntime(17868):     ... 4 more
11-04 21:58:09.872: ERROR/WindowManager(17868): Activity com.Testapp4.BoardIndex has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40562468 that was originally added here
11-04 21:58:09.872: ERROR/WindowManager(17868): android.view.WindowLeaked: Activity com.Testapp4.BoardIndex has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40562468 that was originally added here
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.view.ViewRoot.<init>(ViewRoot.java:275)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.view.Window$LocalWindowManager.addView(Window.java:430)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.Dialog.show(Dialog.java:288)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at com.Testapp4.BoardIndex$pbar.onPreExecute(BoardIndex.java:66)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.os.AsyncTask.execute(AsyncTask.java:391)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at com.Testapp4.BoardIndex.Load(BoardIndex.java:191)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at com.Testapp4.BoardIndex.onCreate(BoardIndex.java:56)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.os.Looper.loop(Looper.java:143)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at android.app.ActivityThread.main(ActivityThread.java:4196)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at java.lang.reflect.Method.invoke(Method.java:507)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-04 21:58:09.872: ERROR/WindowManager(17868):     at dalvik.system.NativeStart.main(Native Method)

I've included almost every jar, while a couple has been removed because they were responsible for Eclipse crashing. I suppose that I've included everything necessary for that 2 lines of code.

In practice, HtmlUnit is experiencing difficulties to work on Android.

Any advice would be greatly appreciated.

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
texasman1979
  • 473
  • 1
  • 6
  • 17
  • were you ever able to get it working? I got through your errors above by creating a libs folder in my project, moving all the jars to there. I also increased the heap size in eclipse(forgot how exactly but it was a simple edit to the eclipse ini file) but got stuck error converting to dalvik. I was never able to get html unit working from there – Joe Yahchouchi May 31 '12 at 11:33
  • Thus far- similar result for me. I got the dalvik error to go away somehow (just disappeared, don't know why), but now I have NoClassDefFoundError for com.gargoylesoftware.htmlunit.WebClient – bhekman Jun 20 '12 at 01:58
  • what you probably did is remove the xerece library(or something) from the jars, that removes the dalvik error, but this jar contains classes that webclient needs so it won't work... I did try all combinations... what I did not try is removing specific classes from the library that causes the dalvik error and seeing if that works. it is time consuming – Joe Yahchouchi Jun 20 '12 at 12:31
  • Yeah, I had removed the xml library to fix the dalvik error 1. And I agree,that would be the way to fix the problem. Did you end up using an alternative solution? – bhekman Jun 20 '12 at 14:17
  • Nope did not solve the problem in any way. was looking into selenium or robotium or whatever but they did not seem as simple and practical. I also tried the apache http get and post, but I hit walls there too – Joe Yahchouchi Jun 20 '12 at 23:19
  • Yeah, I gave up on htmlunit as well. Also hit walls with the standard Android httpclient. Checking into SL4A now- will post ongoing results here: http://stackoverflow.com/questions/11093130/scraping-dynamically-generated-html-inside-android-app – bhekman Jun 20 '12 at 23:35

1 Answers1

3

If you just want to get a page and maybe fill out some forms and submit it, jsoup is a good alternative.

I got it working in android with no problems, just add the jsoup.jar to the libs folder.

Here is the simple example

Document doc = Jsoup.connect("http://jsoup.org/")
            .timeout(4000)
            .get();

Or an example submitting data

Connection.Response res = Jsoup.connect("http://example.org")
                .data("q", username, "password", password) 
                .method(Method.GET)
                .execute();

Document doc = res.parse();
System.out.println(doc);
André Ricardo
  • 3,051
  • 7
  • 23
  • 32