100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 获取java返回的数据_java调用第三方接口 获取接口返回的数据。

获取java返回的数据_java调用第三方接口 获取接口返回的数据。

时间:2020-06-27 12:24:06

相关推荐

获取java返回的数据_java调用第三方接口 获取接口返回的数据。

java接收远程调用的数据,得到的是如上个数的返回内容,我怎么写才能获取到值,现在使用的请求方法如下:

public static HttpResult postJsonData(String url, Map params, String charset) throws Exception{

CloseableHttpClient httpclient = HttpClientUtil.createDefault();

HttpPost httpPost = new HttpPost(url);

//拼接参数

List list = new ArrayList();

for (Map.Entry entry : params.entrySet()) {

String key = entry.getKey().toString();

String value = entry.getValue().toString();

System.out.println("key=" \+ key + " value=" \+ value);

NameValuePair pair = new BasicNameValuePair(key, value);

list.add(pair);

}

CloseableHttpResponse response=null;

try {

if(StringUtils.isEmpty(charset)){

charset = DEFAULT\_CHARSET;

}

httpPost.setEntity(new UrlEncodedFormEntity(list,charset));

response = httpclient.execute(httpPost);

/\*\*请求发送成功,并得到响应\*\*/

if(response!=null && response.getStatusLine().getStatusCode() == HttpStatus.SC\_OK){

HttpEntity httpEntity = response.getEntity();

if (httpEntity!=null){

System.out.println("响应内容为:" \+ EntityUtils.toString(httpEntity));

//System.out.println("响应内容为1:" + httpEntity.getContent());

}

String result=null;

try {

result = EntityUtils.toString(httpEntity,DEFAULT\_CHARSET);

logger.info(result);

} catch (IOException e) {

throw e;

} finally {

EntityUtils.consume(httpEntity);

}

return JSON.parseObject(result,HttpResult.class);

//return httpEntity.getContent();

}else{

return null;

}

} catch (IOException e) {

throw new Exception(e.getMessage());

}finally {

try{

// 释放资源

if(response!=null){

response.close();

}

if(httpclient!=null){

httpclient.close();

}

}catch (IOException e1){

throw new Exception("CloseableHttpResponse close exception");

}

}

}

求教怎么写才能把返回的值取到,现在用上边的方法会报如下错误:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。