JAVA的HttpResponse怎么实例化,org.apache.http.HttpResponse;

开发JAVA调用其他系统的接口,实例化HttpResponse是报错提示:CannotinstantiatethetypeHttpResponse,代码是抄别人网上介绍的... 开发JAVA调用其他系统的接口,实例化HttpResponse 是报错提示:Cannot instantiate the type HttpResponse,代码是抄别人网上介绍的,地址如下:https://www.cnblogs.com/ifindu-san/p/9277967.html ,不知道是哪里出了问题,下载的jar如图;部分代码如下import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete;import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; //import org.caeit.cloud.dev.entity.HttpResponse; // no usepublic class HttpClientUtil { /** * 发送http get请求 */ public static HttpResponse httpGet(String url, Map<String, String> headers, String encode) { HttpResponse response = new HttpResponse(); if (encode == null) { encode = "utf-8"; } String content = null; // since 4.3 不再使用 DefaultHttpClient CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); // 设置header if (headers != null && headers.size() > 0) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpGet.setHeader(entry.getKey(), entry.getValue()); } } CloseableHttpResponse httpResponse = null; try { httpResponse = closeableHttpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); content = EntityUtils.toString(entity, encode); response.setBody(content); response.setHeaders(httpResponse.getAllHeaders()); response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase()); response.setStatusCode(httpResponse.getStatusLine().getStatusCode()); } catch (Exception e) { e.printStackTrace(); } finally { try { httpResponse.close(); } catch (IOException e) { e.printStackTrace(); } } try { // 关闭连接、释放资源 closeableHttpClient.close(); } catch (IOException e) { e.printStackTrace(); } return response; } 展开
 我来答
百度网友13112cc
2019-06-27 · 超过42用户采纳过TA的回答
知道小有建树答主
回答量:241
采纳率:0%
帮助的人:30.1万
展开全部
这里的 HttpResponse 是一个接口,是抽象的,不能这样实例化。你如果要发送http请求,把这句删了不影响,可以如下所用
HttpResponse httpResponse = httpClient.execute(httpPost);
追问

httpClient、httpPost是哪个对象,能否写明细清楚点,麻烦了。

追答

这是一个简单的示范,其实就是发送http请求,建立httpclien。

如果很多参数不懂的话,建议看一下httpClien的方法,网上很多回答,我就不献丑了

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式