ClientWithResponseHandler 示例给出错误?

发布于 2024-12-13 14:32:15 字数 1490 浏览 2 评论 0原文

我很努力地尝试解决这个问题,但我做不到。我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-12-20 14:32:15

我也遇到了同样的错误。我通过添加“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文