在 Android 中使用 HTMLUNit 时出错
您好,我只是尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道解决方案,但现在我对这个问题有了更多的了解。
我使用以下活动代码创建了一个简单的“hello world”应用程序:
我将以下 jar 添加到 Eclipse 项目:(
并且我必须手动从 JUnit jar 中删除 License.txt 文件,否则该项目将无法构建。)
然后我尝试使用 Android SDK 2.2 和 2.3 在模拟器中运行,在这两种情况下都看到 OP 报告的相同异常。 (并看到模拟器中显示的异常消息。)
更改浏览器版本没有效果,关闭 Javascript 也没有效果。请求的 URL 也无关紧要,因为在 WebClient 设置期间会引发异常。
异常发生在
ClassConfiguration
的第 105 行:使用调试器跟踪此情况,我看到
hostClass_
遍历了许多值(包括com.gargoylesoftware.htmlunit.javascript .host.BoxObject
和com.gargoylesoftware.htmlunit.javascript.host.Attr
)没有问题。只有当到达com.gargoylesoftware.htmlunit.javascript.host.css.CSSImportRule
时才会出现问题。奇怪的是,该方法确实存在:
不幸的是,我找不到 SDK 提供的
java.lang.Class
源代码(我 找到了 SDK 其他部分的来源,但不是 JRE),但是盲目地单步执行代码,我可以看到getMethod
,包括调用Class.checkPublicMemberAccess
、Class.getClassCache
和ClassCache.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:
I added the following jars to Eclipse project:
(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
:Tracing this with the debugger, I saw
hostClass_
go through a number of values (includingcom.gargoylesoftware.htmlunit.javascript.host.BoxObject
andcom.gargoylesoftware.htmlunit.javascript.host.Attr
) with no problems. It's only when it gets tocom.gargoylesoftware.htmlunit.javascript.host.css.CSSImportRule
that the problems start.Strangely, the method DOES exist:
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 insidegetMethod
, including calls toClass.checkPublicMemberAccess
,Class.getClassCache
, andClassCache.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?