Android:使用 HTML Unit & 时 CSS 错误蚀

发布于 2024-12-23 15:07:18 字数 3049 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

じее 2024-12-30 15:07:18

我认为您在 Eclipse 中的类路径有问题:

Could not find class 'org.w3c.dom.css.CSSCharsetRule'

您是否将所有 Jars 都包含到构建路径中?

Conversion to Dalvik Format Failed with Error 1

这告诉我,你的 *.class 之一无法转换为 *dex 文件

也许你有两个包含相同类的 jar 文件:

我解决了这个问题。

我的构建路径上似乎有两个包含相同内容的 jar
包和类。

“转换为 Dalvik 格式失败,错误 1”在外部 JAR 上

编辑
要查找重复的类,请使用您最喜欢的解压缩工具提取所有 jar 并等待“此文件已存在”。

I think you have a problem with you classpath within eclipse:

Could not find class 'org.w3c.dom.css.CSSCharsetRule'

Do you have all Jars included to the buildpath?

Conversion to Dalvik Format Failed with Error 1

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:

I solved the problem.

It seems that I have two jars on my buildpath that include the same
package and classes.

"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".

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