//设置参数 List<NameValuePair> list = new ArrayList<NameValuePair>(); Iterator iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Entry<String,String> elem = (Entry<String, String>) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(),elem.getValue())); } if(list.size() > 0){ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset); httpPost.setEntity(entity); }
根据分析原因大概有两种
原因:1)前端提交数据的字段名称或者是字段类型和后台的实体类不一致,导致无法封装;
在检查字段名无误之后猜就是第二点了,我就去找其他的设置入参的方法,替换,如下
//设置参数 JSONObject jsonObject = new JSONObject(); for (Map.Entry<String,String> mt:map.entrySet()){ jsonObject.put(mt.getKey(),mt.getValue()); } httpPost.setEntity(new StringEntity(jsonObject.toString()));接着就是正常的返回,暂时没时间查找第一种设置参数失败的具体原因,碰到的还第二种吧。
文章来源: HttpClient 返回400处理