【问题标题】:ClientWithResponseHandler example Gives an ERROR?ClientWithResponseHandler 示例给出错误?
【发布时间】:2011-11-03 11:38:09
【问题描述】:

我已经努力解决这个问题,但我无法解决。我正在尝试使用来自 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);” IT 说它找不到方法 execute(HttpGet,ResponseHandler) 这个问题不应该是例子吗?我究竟做错了什么?! :S

【问题讨论】:

    标签: httpclient execute http-get


    【解决方案1】:

    我也遇到了同样的错误。我通过添加“httpcore-4.2.1.jar”解决了这个问题。然后它开始抱怨找不到用于记录的 Class Def。所以我添加了“commons-logging-1.1.1.jar”,现在我认为它工作正常。这两个文件都可以与“httpclient-4.2.1.jar”一起找到。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2021-03-28
      • 2020-04-02
      • 1970-01-01
      相关资源
      最近更新 更多