【问题标题】:Can't import Apache HTTP in Eclipse无法在 Eclipse 中导入 Apache HTTP
【发布时间】:2014-08-12 23:41:53
【问题描述】:

我正在使用 Apache 文档中的示例代码,如下所示:

        final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();

    webClient.closeAllWindows();

我收到来自 WebClient、HtmlPage、HtmlForm 的错误...基本上,应该从 Apache 导入的所有内容都没有。

我已经按照网络上的一堆指南指导我在 hwo 上将外部 jar 添加到我的构建路径中,据我所知,一切都设置正确。我试过清理项目、重新启动 Eclipse、关闭/打开项目等。似乎没有任何效果。

作为参考,我从这个网站下载了 .JAR:http://hc.apache.org/

我的导入语句:

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

我的错误信息:

[2014-06-23 12:43:55 - CampusHappenings] Dx 1 错误;中止 [2014-06-23 12:43:55 - CampusHappenings] 转换为 Dalvik 格式失败,出现错误 1

整个堆栈跟踪太长而无法复制,但它与 htmlunit JAR 本身有关,与我的代码无关。它们看起来都像这样:

Dx 警告:忽略匿名内部类的 InnerClasses 属性 (org.apache.xalan.lib.sql.SecuritySupport12$4) 不附带 关联的 EnclosureMethod 属性。这个类可能是由一个 不针对现代 .class 文件格式的编译器。

想法?我很困惑。

【问题讨论】:

  • 你是如何将 jar 添加到构建路径的?
  • 我想查看您的代码的完整内容。它有助于消除所有简单的“哎呀”修复。
  • 就像@FrankieTheKneeMan 所说,我们至少应该看到您的导入语句。这就是你所说的“进口”,对吧? 究竟您看到的错误消息是什么?
  • 你能把你的堆栈跟踪吗?
  • 达尔维克?这是否意味着您正在为 Android 编写代码?

标签: java eclipse apache


【解决方案1】:

由于我在错误日志中看到了 Dalvik,我假设您正在为 Android 编写代码,但尝试使用可能与 Android 不兼容的通用 Apache HttpClient 版本。你真的应该检查这个 Apache 的文档并尝试他们专门针对 Android 的 4.3 端口。

https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html

Android jar 的 HttpClient 可以在这里找到:http://search.maven.org/remotecontent?filepath=org/apache/httpcomponents/httpclient-android/4.3.3/httpclient-android-4.3.3.jar

【讨论】:

  • 甚至没有意识到 Android 有一个单独的东西。我去看看,谢谢我没有看到用于此的 JAR,这就是我习惯向 Eclipse 添加东西的方式,而且我从未听说过 Maven。我将如何将此客户端集成到我的项目中?
  • 我讨厌 Android 用户对 Java 的投机取巧的做法,并违反其“​​一次编写,到处运行”的主要原则,但我对此无能为力,除了咆哮 :)
  • 如果您不使用 maven,请尝试从此处下载 jar:search.maven.org/#search|ga|1|a%3A%22httpclient-android%22 您需要单击“jar”链接开始下载。
  • 明白,谢谢。无论如何,我最终还是放弃了使用 Apache HTTP,因为显然不建议 Android 开发人员使用它。不过感谢您的帮助。
猜你喜欢
  • 2023-04-02
  • 2011-12-16
  • 2015-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-23
相关资源
最近更新 更多