获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 创建Post请求 HttpPost httpPost = new HttpPost(WcsHttpUtil.httpUtil.agvs); // 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json) httpPost.setHeader("Content-Type", "application/json;charset=utf8"); // 响应模型 CloseableHttpResponse response = null; String jsonString = JSON.toJSONString(object); StringEntity entity = new StringEntity(jsonString, "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 httpPost.setEntity(entity); // 由客户端执行(发送)Post请求 response = httpClient.execute(httpPost); // 从响应模型中获取响应实体 HttpEntity responseEntity = response.getEntity(); java类接收
时间: 2023-11-28 18:47:55 浏览: 38
你可以参考以下代码,将接口返回的结果转换为Java对象:
```
// 响应实体转字符串
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
// 将字符串转换为Java对象
MyResponseObject responseObject = JSON.parseObject(responseString, MyResponseObject.class);
```
其中,`MyResponseObject`是你定义的Java类,用于接收接口返回的数据。你需要根据接口返回的数据结构定义该类的属性及其类型。
相关问题
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 创建Post请求 HttpPost httpPost = new HttpPost(WcsHttpUtil.httpUtil.agvs); // 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json) httpPost.setHeader("Content-Type", "application/json;charset=utf8"); // 响应模型 CloseableHttpResponse response = null; String jsonString = JSON.toJSONString(object); StringEntity entity = new StringEntity(jsonString, "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 httpPost.setEntity(entity); // 由客户端执行(发送)Post请求 response = httpClient.execute(httpPost); // 从响应模型中获取响应实体 HttpEntity responseEntity = response.getEntity(); 完善此代码接收数据为XML
可以将代码中的 "Content-Type" 改成 "application/xml;charset=utf-8",并且将响应实体的内容转换为XML格式。修改后的代码如下所示:
```
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Post请求
HttpPost httpPost = new HttpPost(WcsHttpUtil.httpUtil.agvs);
// 设置ContentType为XML格式
httpPost.setHeader("Content-Type", "application/xml;charset=utf-8");
// 响应模型
CloseableHttpResponse response = null;
String xmlString = /* 将对象转换为XML格式的字符串 */ ;
StringEntity entity = new StringEntity(xmlString, "UTF-8");
// 将entity放入post请求体中
httpPost.setEntity(entity);
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
// 将响应实体的内容转换为XML格式
String xmlResponse = EntityUtils.toString(responseEntity, "UTF-8");
```
@JacksonXmlRootElement(localName = "BillHead") public class BillHead { //企业内部编号 @JacksonXmlProperty(localName = "CopListNo") private String CopListNo; //企业编号 @JacksonXmlProperty(localName = "CopNo") private String CopNo; } 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 创建Post请求 HttpPost httpPost = new HttpPost(WcsHttpUtil.httpUtil.agvs); // 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json) httpPost.setHeader("Content-Type", "application/json;charset=utf8"); // 响应模型 CloseableHttpResponse response = null; String jsonString = JSON.toJSONString(object); StringEntity entity = new StringEntity(jsonString, "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 httpPost.setEntity(entity); // 由客户端执行(发送)Post请求 response = httpClient.execute(httpPost); // 从响应模型中获取响应实体 HttpEntity responseEntity = response.getEntity(); 完善此代码使用BillHead接收
可以在代码中添加以下部分来使用BillHead接收响应:
```
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(WcsHttpUtil.httpUtil.agvs);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 创建请求数据对象
BillHead billHead = new BillHead();
billHead.setCopListNo("123456");
billHead.setCopNo("789");
// 将请求数据对象转换为JSON字符串
String jsonString = JSON.toJSONString(billHead);
StringEntity entity = new StringEntity(jsonString, "UTF-8");
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应数据JSON字符串转换为BillHead对象
String responseString = EntityUtils.toString(responseEntity);
BillHead responseBillHead = JSON.parseObject(responseString, BillHead.class);
// 打印接收到的BillHead对象
System.out.println(responseBillHead);
}
} finally {
response.close();
}
```