我有这个程序:
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
public class ApplicationRESTFul {
public static void main(String[] args) {
String url = "http://www.google.com/search?q=httpClient";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.addHeader("Accept", "application/json");
}
}
但是我从eclipse得到了这条消息
the method addHeader (String, String) is undefined for the type HttpGet
我正在使用这个库,正如我在文档中看到的那样,该方法应该存在(org.apache.httpcomponents.httpclient_4.5)
答案 0 :(得分:3)
我通过将httpcore JAR添加到类路径来解决它。从maven添加依赖项也会添加httpcore JAR,而不仅仅是httpclient JAR,这也是它的原因。