平时大多数用的HTTP POST 请求时这样的装填参数方式
// 装填参数
List nvps = new ArrayList();
if (map != null) {
for (Entry entry : map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
}
}
// 设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
但是有些POST 请求,需要使用JSON的方式进行传参,那应该时这样子的
JSONObject json = new JSONObject();
StringEntity postingString = new StringEntity(json.toString());// json传递
// 设置参数到请求对象中
httpPost.setEntity(postingString);
httpPost.setHeader("Content-type", "application/json");
昨天第一次调用企业微信的接口就遇到这个问题,找了很多地方,才无意中发现时这样子的。
特此发出来给和我一样的小白查看