ClientWithResponseHandler 示例给出错误?
我很努力地尝试解决这个问题,但我做不到。我正在尝试使用 apache 的 httpClient 4.1.2。按照我从示例开始的逻辑,问题是我遇到了一些我不理解的奇怪错误。问题是这样的:
package ClientWithResponseHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
public class Main {
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.google.com/");
System.out.println("executing request " + httpget.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = **httpclient.execute(httpget, responseHandler);**
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
错误在于“httpclient.execute(httpget, responseHandler);”它说它找不到方法execute(HttpGet,ResponseHandler) 问题是这个例子不应该起作用吗?我做错了什么?! :S
I've tried hard to solve this and I couldn't. I'm trying to use httpClient 4.1.2 from apache. As logic I started with the example, the problem is that I'm having some strange error that I don't understand. This is the deal:
package ClientWithResponseHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
public class Main {
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.google.com/");
System.out.println("executing request " + httpget.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = **httpclient.execute(httpget, responseHandler);**
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
The error is with "httpclient.execute(httpget, responseHandler);" IT says that it cannot find the method execute(HttpGet,ResponseHandler)
The question shouldn't the example work? What am I doing wrong?! :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到了同样的错误。我通过添加“httpcore-4.2.1.jar”解决了这个问题。然后它开始抱怨找不到用于记录的 Class Def。所以我添加了“commons-logging-1.1.1.jar”,现在我认为它工作得很好。这两个文件都可以与“httpclient-4.2.1.jar”一起找到。
希望这有帮助。
I got the same error too. I resolved it by adding the "httpcore-4.2.1.jar". Then it started complaining about the Class Def not found for logging. So I added "commons-logging-1.1.1.jar" and now I think it works fine. Both these files can be found along with "httpclient-4.2.1.jar".
Hope this helps.