我使用的是 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 导入的内容都没有。
我遵循了网上的一堆指南,指导我将外部 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 文件格式的编译器。
想法?我很困惑。
最佳答案
由于我在错误日志中看到了 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
关于java - 无法在 Eclipse 中导入 Apache HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24357354/