Java 无法实例化类型 HttpClient
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3276903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot instantiate the type HttpClient
提问by Aymon Fournier
I have added the .jars to my library and I can also instantiate other classes in the JAR. What gives? I've tried to clean the project etc.
我已经将 .jars 添加到我的库中,我还可以在 JAR 中实例化其他类。是什么赋予了?我试图清理项目等。
It's a compiler-time error. Eclipse won't let me instantiate it.
这是一个编译器时错误。Eclipse 不会让我实例化它。
I'm importing the correct library, not the sun version, and using the default contructor, specified by their tutorial
我正在导入正确的库,而不是 sun 版本,并使用他们的教程指定的默认构造函数
HttpClient client = new HttpClient();
(Eclipse, mac, Apache HTTP, "HttpClient 4.0.1 (GA)" downloaded from here)
(Eclipse、mac、Apache HTTP、从这里下载的“HttpClient 4.0.1 (GA)” )
采纳答案by b_erb
HttpClient
is an interface in 4.x, use DefaultHttpClient
for instances.
HttpClient
是 4.x 中的接口,DefaultHttpClient
用于实例。
HttpClient httpclient = new DefaultHttpClient();
回答by Aymon Fournier
HttpClient client = new DefaultHttpClient();
They haven't documented this anywhere on the website, but I've imported the source, and the javadoc, and this was the example in the Javadoc for the HttpClient class.
他们没有在网站上的任何地方记录这一点,但我已经导入了源代码和 javadoc,这是 HttpClient 类的 Javadoc 中的示例。
回答by Artemis
Put in ayour attention that depending on your demands you should instance httpCleint with HttpParams, and/or connection manager. It can be useful in case of thultithreading access, when default settings doesn't fit.
请注意,根据您的需求,您应该使用 HttpParams 和/或连接管理器实例化 httpCleint。当默认设置不适合时,它在 thultithreading 访问的情况下很有用。
You can find more info in documentation here http://hc.apache.org/httpcomponents-client-ga/
您可以在此处的文档中找到更多信息http://hc.apache.org/httpcomponents-client-ga/
回答by pablosaraiva
If you are using 4.3, one way to initialize it is:
如果您使用的是 4.3,初始化它的一种方法是:
CloseableHttpClient httpclient = HttpClients.createDefault();
The quick start guide for 4.3 is located at:
4.3 的快速入门指南位于:
http://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html
http://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html