Android:使用 HTML Unit & 时 CSS 错误蚀
我正在使用 Html Unit 访问网页,填写一些表格,然后单击按钮转到下一个网页。这个新网页(在我的代码中称为 newPage)是我从中提取 html 源的内容。
此方法在 Netbeans 中完美运行(尽管它确实给出了过多的警告)
import org.jsoup.Jsoup;
/* Regular (textview, button, etc) imports */
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
String username = "USERNAME", school = "SCHOOL", userpassword = "PASSWORD";
HtmlElement loginName = null;
HtmlElement password = null;
//HtmlPage newPage;
HtmlAnchor anchorByHref = null;
try {
WebClient webClient = new WebClient();
final HtmlPage page = webClient
.getPage("https://cupertino.schoolloop.com/portal/login?d=x&return_url=1325402408153");
loginName = page.getElementByName("login_name");
password = page.getElementByName("password");
loginName.setTextContent(username);
loginName.setAttribute("value", username);
password.setAttribute("value", userpassword);
password.setTextContent(userpassword);
anchorByHref = page
.getAnchorByHref("javascript:document.form.event_override.value='login';document.form.submit();");
HtmlPage newPage = anchorByHref.click();
webClient.waitForBackgroundJavaScript(10000);
String html = newPage.getWebResponse().getContentAsString();
message2.setText(html);
} catch (Exception e) {
e.printStackTrace();
}
}
但是,在运行 Android 的 Eclipse 中,它
01-02 17:38:54.099: E/dalvikvm(23163): Could not find class 'org.w3c.dom.css.CSSCharsetRule', referenced from method com.gargoylesoftware.htmlunit.javascript.host.css.CSSCharsetRule.getCharsetRule
在 logcat 中显示。
另外,它没有将 message2 的文本设置为我想要的 html,尽管当我在 Netbeans 中尝试此操作并打印“html”时,它实际上打印了页面的完整 html 源代码!
总结起来,有2个问题。 Eclipse 没有成功获取 html,并且给出错误,指出找不到 CSSCharsetRule 类。我想我已经为此导入了适当的 .jar,但我不确定,因为 HTML Unit 附带了大约 15 个 .jar,我无法全部导入它们,否则应用程序在模拟器和手机中会出现问题。
I have figured out that the error was due to not importing xml-apis-1.3.04.jar (which was imported in the Netbeans Project).
然而,现在我遇到了一个
“转换为 Dalvik 格式失败,错误 1”
错误。
是否有任何问题会导致 html 为 Null 值,如何修复 Dalvik 转换错误?
.jar Eclipse(太多会使应用程序变得繁重,并且包含 xml-api 会导致 Dalvik 错误)
Netbeans 中的 Jar 文件(工作)
I am using Html Unit to go to a webpage, fill in some forms, and click a button to get to the next webpage. That new webpage, (called newPage in my code) is what I'm extracting the html source from.
This method works perfectly in Netbeans (although it does give an excessive amount of warnings)
import org.jsoup.Jsoup;
/* Regular (textview, button, etc) imports */
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
String username = "USERNAME", school = "SCHOOL", userpassword = "PASSWORD";
HtmlElement loginName = null;
HtmlElement password = null;
//HtmlPage newPage;
HtmlAnchor anchorByHref = null;
try {
WebClient webClient = new WebClient();
final HtmlPage page = webClient
.getPage("https://cupertino.schoolloop.com/portal/login?d=x&return_url=1325402408153");
loginName = page.getElementByName("login_name");
password = page.getElementByName("password");
loginName.setTextContent(username);
loginName.setAttribute("value", username);
password.setAttribute("value", userpassword);
password.setTextContent(userpassword);
anchorByHref = page
.getAnchorByHref("javascript:document.form.event_override.value='login';document.form.submit();");
HtmlPage newPage = anchorByHref.click();
webClient.waitForBackgroundJavaScript(10000);
String html = newPage.getWebResponse().getContentAsString();
message2.setText(html);
} catch (Exception e) {
e.printStackTrace();
}
}
However, in Eclipse running Android, it says
01-02 17:38:54.099: E/dalvikvm(23163): Could not find class 'org.w3c.dom.css.CSSCharsetRule', referenced from method com.gargoylesoftware.htmlunit.javascript.host.css.CSSCharsetRule.getCharsetRule
in the logcat.
Also, it doesn't set message2's text as the html I want although when I try this in Netbeans and print "html" it does in fact print the full html source of the page!
In summary, there are 2 Problems. Eclipse is not successfully getting the html, and it is giving an error saying could not find CSSCharsetRule class. I think I have imported the appropriate .jar for this, but I am not certain because HTML Unit comes with around 15 .jars and I cannot import them all otherwise the application has trouble in emulator and phone.
I have figured out that the error was due to not importing xml-apis-1.3.04.jar (which was imported in the Netbeans Project).
However, now I encounter a
"Conversion to Dalvik Format Failed with Error 1"
Error.
Is it possible that any of the problems would cause the Null value of html, and how do I fix the Dalvik Conversion Error?
.jar Eclipse(too many makes app heavy, and including xml-apis causes Dalvik Error)
Jar files in Netbeans (working)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您在 Eclipse 中的类路径有问题:
您是否将所有 Jars 都包含到构建路径中?
这告诉我,你的 *.class 之一无法转换为 *dex 文件
也许你有两个包含相同类的 jar 文件:
“转换为 Dalvik 格式失败,错误 1”在外部 JAR 上
编辑
要查找重复的类,请使用您最喜欢的解压缩工具提取所有 jar 并等待“此文件已存在”。
I think you have a problem with you classpath within eclipse:
Do you have all Jars included to the buildpath?
This tells me, that one of your *.class can't be convertet into a *dex-file
Maybe you have two jar-files with the same class inside:
"Conversion to Dalvik format failed with error 1" on external JAR
edit
To find duplicate Classes, extract all jars with your favourite unzip-tool and wait for "This file aready exists".