HtmlUnit Android problem with WebClient



HtmlUnit是惊人的,至少在Java中我没有遇到任何问题。不幸的是,当切换到Android平台的代码,它给我错误,当我试图创建一个web客户端。

import android.app.Activity;
import android.os.Bundle;
import com.gargoylesoftware.htmlunit.WebClient;
public class AndroidTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final WebClient webClient = new WebClient();
    }
}

程序在AVD上启动前给我的警告。

这是它给我的一个警告的例子,这就是警告但是它一遍又一遍地重复了大约100000x次:

 [2011-08-31 21:25:24 - AndroidTest] Dx warning: Ignoring InnerClasses attribute for an anonymous inner class (org.apache.commons.collections.BeanMap$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is not an inner class.

你能给我什么帮助吗?

android调试器错误:

09-01 07:44:24.569: WARN/dalvikvm(877): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-01 07:44:24.600: ERROR/AndroidRuntime(877): FATAL EXCEPTION: main
09-01 07:44:24.600: ERROR/AndroidRuntime(877): java.lang.ExceptionInInitializerError
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at com.Test.AndroidTestActivity.onCreate(AndroidTestActivity.java:20)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.os.Looper.loop(Looper.java:123)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at java.lang.reflect.Method.invoke(Method.java:521)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at dalvik.system.NativeStart.main(Native Method)
09-01 07:44:24.600: ERROR/AndroidRuntime(877): Caused by: java.lang.NoClassDefFoundError: org.apache.commons.lang.StringUtils
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at com.gargoylesoftware.htmlunit.util.URLCreator$URLCreatorStandard.toUrlUnsafeClassic(URLCreator.java:66)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlUnsafe(UrlUtils.java:193)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlSafe(UrlUtils.java:171)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     at       com.gargoylesoftware.htmlunit.WebClient.<clinit>(WebClient.java:162)
09-01 07:44:24.600: ERROR/AndroidRuntime(877):     ... 14 more

**添加common_lang和REST后出现错误

09-02 16:16:34.440: ERROR/AndroidRuntime(396): FATAL EXCEPTION: main
09-02 16:16:34.440: ERROR/AndroidRuntime(396): java.lang.NoClassDefFoundError: com.gargoylesoftware.htmlunit.DefaultCssErrorHandler
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:157)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:180)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at com.Test.AndroidTestActivity.onCreate(AndroidTestActivity.java:21)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.os.Looper.loop(Looper.java:123)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at java.lang.reflect.Method.invokeNative(Native Method)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at java.lang.reflect.Method.invoke(Method.java:521)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-02 16:16:34.440: ERROR/AndroidRuntime(396):     at dalvik.system.NativeStart.main(Native Method)

我也有类似的问题。经过一番研究,我发现这是由于名称空间冲突造成的。你必须重新编译htmlunit的源代码。我现在试着这样做,但是htmlunit太大太复杂了,我还没有完成。

你的stacktrace显示一个ExceptionInInitializerError被抛出,它是由以下原因引起的:

Caused by: java.lang.NoClassDefFoundError: org.apache.commons.lang.StringUtils

也许你在类路径中缺少一些JAR (common -lang) ?

有一个特殊的Android版本的HtmlUnit (https://github.com/HtmlUnit/htmlunit-android),应该可以解决大多数与Android jdk中不存在类相关的问题。

页面上还有一个关于common -lang问题的提示。

使用此方法还需要将源代码更新为HtmlUnit 3 -参见https://www.htmlunit.org/migration.html获得一些提示。

最新更新