在 Android 中使用 HTMLUNit 时出错

发布于 2024-11-08 03:22:58 字数 1817 浏览 0 评论 0原文

您好,我只是尝试使用 htmlunit 站点上的示例代码加载页面,即:

final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.google.com");

但我总是在上述代码的第二行收到此错误。我已经包含了该库。可能是什么问题。我搜索了将近两天,但停留在同一个部分。请帮忙。

05-20 10:58:11.322: ERROR/JavaScriptEngine(960): Exception while initializing JavaScript for the page
05-20 10:58:11.322: ERROR/JavaScriptEngine(960): java.lang.IllegalStateException: Method 'jsxGet_href' was not found for href property in com.gargoylesoftware.htmlunit.javascript.host.css.CSSImportRule
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration.addProperty(ClassConfiguration.java:109)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.parsePropertyElement(JavaScriptConfiguration.java:402)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.parseClassElement(JavaScriptConfiguration.java:349)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.buildUsageMap(JavaScriptConfiguration.java:299)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.<init>(JavaScriptConfiguration.java:149)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.getInstance(JavaScriptConfiguration.java:239)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.init(JavaScriptEngine.java:179)

Hi I am simply trying to load a page using the sample code at htmlunit site which is:

final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.google.com");

But I am always getting this error on the second line of the above code. I have the lib included. What can be the problem. I searched for almost 2 days but stuck on the same part. Please help.

05-20 10:58:11.322: ERROR/JavaScriptEngine(960): Exception while initializing JavaScript for the page
05-20 10:58:11.322: ERROR/JavaScriptEngine(960): java.lang.IllegalStateException: Method 'jsxGet_href' was not found for href property in com.gargoylesoftware.htmlunit.javascript.host.css.CSSImportRule
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration.addProperty(ClassConfiguration.java:109)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.parsePropertyElement(JavaScriptConfiguration.java:402)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.parseClassElement(JavaScriptConfiguration.java:349)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.buildUsageMap(JavaScriptConfiguration.java:299)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.<init>(JavaScriptConfiguration.java:149)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.getInstance(JavaScriptConfiguration.java:239)
05-20 10:58:11.322: ERROR/JavaScriptEngine(960):     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.init(JavaScriptEngine.java:179)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是男神闪亮亮 2024-11-15 03:22:58

我不知道解决方案,但现在我对这个问题有了更多的了解。

我使用以下活动代码创建了一个简单的“hello world”应用程序:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv = new TextView(this);

    try
    {
        final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
        webClient.setJavaScriptEnabled( false ) ;
        final HtmlPage page = webClient.getPage("http://www.google.com");

        tv.setText( page.asText().substring( 0, 50 ) );
    }
    catch( Throwable t )
    {
        tv.setText(t.getMessage());
    }

    setContentView(tv);

我将以下 jar 添加到 Eclipse 项目:(

apache-mime4j-0.6.jar
commons-codec-1.4.jar
commons-collections-3.2.1.jar
commons-io-1.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
cssparser-0.9.5.jar
htmlunit-2.8.jar
htmlunit-core-js-2.8.jar
httpclient-4.0.1.jar
httpcore-4.0.1.jar
httpmime-4.0.1.jar
nekohtml-1.9.14.jar
sac-1.3.jar
serializer-2.7.1.jar
commons-configuration-1.6.jar
junit-4.5.jar

并且我必须手动从 JUnit jar 中删除 License.txt 文件,否则该项目将无法构建。)

然后我尝试使用 Android SDK 2.2 和 2.3 在模拟器中运行,在这两种情况下都看到 OP 报告的相同异常。 (并看到模拟器中显示的异常消息。)

更改浏览器版本没有效果,关闭 Javascript 也没有效果。请求的 URL 也无关紧要,因为在 WebClient 设置期间会引发异常。

异常发生在 ClassConfiguration 的第 105 行:

info.setReadMethod(hostClass_.getMethod(GETTER_PREFIX + name, (Class []) null));

使用调试器跟踪此情况,我看到 hostClass_ 遍历了许多值(包括 com.gargoylesoftware.htmlunit.javascript .host.BoxObjectcom.gargoylesoftware.htmlunit.javascript.host.Attr)没有问题。只有当到达 com.gargoylesoftware.htmlunit.javascript.host.css.CSSImportRule 时才会出现问题。

奇怪的是,该方法确实存在:

public String jsxGet_href() {
    return getImportRule().getHref();
}

不幸的是,我找不到 SDK 提供的 java.lang.Class 源代码(我 找到了 SDK 其他部分的来源,但不是 JRE),但是盲目地单步执行代码,我可以看到 getMethod,包括调用Class.checkPublicMemberAccessClass.getClassCacheClassCache.getAllPublicMethods,最后一个会引发异常。

如果没有 JRE 源,很难准确地看出失败的原因,但是......我怀疑存在某种 jar 不兼容问题。也许 HtmlUnit jar 应该使用 Android SDK 重新编译?

I don't know the solution, but I know a bit more about the issue, now.

I created a trivial 'hello world' app with the following activity code:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv = new TextView(this);

    try
    {
        final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
        webClient.setJavaScriptEnabled( false ) ;
        final HtmlPage page = webClient.getPage("http://www.google.com");

        tv.setText( page.asText().substring( 0, 50 ) );
    }
    catch( Throwable t )
    {
        tv.setText(t.getMessage());
    }

    setContentView(tv);

I added the following jars to Eclipse project:

apache-mime4j-0.6.jar
commons-codec-1.4.jar
commons-collections-3.2.1.jar
commons-io-1.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
cssparser-0.9.5.jar
htmlunit-2.8.jar
htmlunit-core-js-2.8.jar
httpclient-4.0.1.jar
httpcore-4.0.1.jar
httpmime-4.0.1.jar
nekohtml-1.9.14.jar
sac-1.3.jar
serializer-2.7.1.jar
commons-configuration-1.6.jar
junit-4.5.jar

(And I had to manually remove the License.txt file from the JUnit jar, or the project would not build.)

Then I tried running in the simulator using Android SDKs 2.2 and 2.3, and in both cases saw the same exception reported by the OP. (And saw the exception message displayed in the simulator.)

Changing the browser version has no effect, nor does turning off Javascript. The requested URL also does not matter, as the exception is thrown during setup of the WebClient.

The exception occurs at line 105 of ClassConfiguration:

info.setReadMethod(hostClass_.getMethod(GETTER_PREFIX + name, (Class []) null));

Tracing this with the debugger, I saw hostClass_ go through a number of values (including com.gargoylesoftware.htmlunit.javascript.host.BoxObject and com.gargoylesoftware.htmlunit.javascript.host.Attr) with no problems. It's only when it gets to com.gargoylesoftware.htmlunit.javascript.host.css.CSSImportRule that the problems start.

Strangely, the method DOES exist:

public String jsxGet_href() {
    return getImportRule().getHref();
}

Unfortunately, I couldn't find the source to java.lang.Class that the SDK provides (I found sources for other parts of the SDK, but not the JRE), but stepping through the code blind, I could see quite a bit of activity inside getMethod, including calls to Class.checkPublicMemberAccess, Class.getClassCache, and ClassCache.getAllPublicMethods, the last of which throws the exception.

Without the JRE source it's hard to see exactly what is failing, but... I suspect some kind of jar incompatibily issue. Perhaps the HtmlUnit jars should be recompiled with Android SDK?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文