这是我的代码:
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");
这是我得到的错误:
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)
我几乎包含了每个jar
,而一对夫妇因为负责Eclipse崩溃而被删除了。我想我已经包含了这两行代码所需的一切。
在实践中,HtmlUnit
在使用Android时遇到了困难。
非常感谢任何建议。
答案 0 :(得分:3)
如果您只想获得一个页面并填写一些表格并提交,jsoup是一个不错的选择。
我在android中没有任何问题,只需将jsoup.jar添加到libs文件夹中。
这是一个简单的例子
Document doc = Jsoup.connect("http://jsoup.org/")
.timeout(4000)
.get();
或提交数据的示例
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);