关于httppost 错误处理

发布时间:2024-12-11 03:37

学会道歉,处理错误的方式 #生活技巧# #人际关系技巧# #恋爱心理辅导#

最新推荐文章于 2024-10-11 17:29:21 发布

三川木 于 2018-12-13 13:25:32 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

关于post请求中map和json的问题:如下如果将方法参数中JSONObject 换成Map会报错,

public static String post(String url,JSONObject jsonParam,Integer connTimeout,Integer readTimeout) throws Exception {

HttpPost httpPost = new HttpPost(url);

CloseableHttpClient client = HttpClients.createDefault();

StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");

entity.setContentEncoding("UTF-8");

entity.setContentType("application/json");

httpPost.setEntity(entity);

RequestConfig.Builder customReqConf = RequestConfig.custom();

if (connTimeout != null) {

customReqConf.setConnectTimeout(connTimeout);

}

if (readTimeout != null) {

customReqConf.setSocketTimeout(readTimeout);

}

httpPost.setConfig(customReqConf.build());

HttpResponse resp = client.execute(httpPost);

return IOUtils.toString(resp.getEntity().getContent(), "UTF-8");

}

即使使用如下 还是报错(JSON Parse error: Unrecognized token '),最终淘汰掉map直接使用的jsonobject,原因不详???

if (map!= null && !map.isEmpty()) {

List<NameValuePair> formParams = new ArrayList<NameValuePair>();

Set<Map.Entry<String, String>> entrySet = map.entrySet();

for (Map.Entry<String, String> entry : entrySet) {

formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));

}

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);

post.setEntity(entity);

}

网址:关于httppost 错误处理 https://www.yuejiaxmz.com/news/view/439723

上一篇:python PyQt5 QVB

相关内容

关于生活中的错误小常识解密
关于饮食有哪些错误方法
下列关于牛黄抱龙丸说法错误的是()
下列关于保险产品在个人理财中的应用,说法错误的是(  )。
dart 错误
IIS 7.5 详细错误
IIS 10.0 详细错误
站点错误
化妆的误区以及细节处理
IIS 8.5 详细错误

随便看看