1、EntityUtils.toString这种东西,由于IO流的关闭,所以只能用一次,要不就直接报错;
2、body部分,setEntity两种格式:
1)Iterator<Entry<String, String>> hjInterator = map.entrySet().iterator();
JSONObject jsonParam = new JSONObject();
while (hjInterator.hasNext()) {
Entry<String, String> elem = hjInterator.next();
jsonParam.put(elem.getKey(), elem.getValue());
}
StringEntity entity = new StringEntity(jsonParam.toString(),“utf-8”);
entity.setContentEncoding(“UTF-8”);
entity.setContentType(“application/json”);
httpPost.setEntity(entity);
2)List list = new ArrayList();
// 循环遍历map,放入list
Iterator<Entry<String, String>> hjInterator = map.entrySet().iterator();
while (hjInterator.hasNext()) {
Entry<String, String> elem = hjInterator.next();
list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
}
// 将list放入httpPost
if (list.size() > 0) {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, “UTF-8”);
httpPost.setEntity(entity);
}
3、httpPost.setHeader(“Content-type”, “application/json;charset=utf-8”);
。。。。。。。。。。怎么说呢,想到哪写哪
HttpPost中的坑
最新推荐文章于 2022-03-12 20:18:07 发布