gpt4 book ai didi

java - 如何让 HtmlUnit 在 Android 下工作?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:47 26 4
gpt4 key购买 nike

这是我的代码:

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 上工作时遇到了困难。

如有任何建议,我们将不胜感激。

最佳答案

如果您只是想获得一个页面并填写一些表格并提交,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);

关于java - 如何让 HtmlUnit 在 Android 下工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8017874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com