获取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");
}
}
}
求教怎么寫才能把返回的值取到,現在用上邊的方法會報如下錯誤:
總結
以上是生活随笔為你收集整理的获取java返回的数据_java调用第三方接口,获取接口返回的数据。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 折线动图_在java中使用jf
- 下一篇: java线程 demo_Java多线程d