private void httpReqUrl(List<HongGuTan> list, String url)
throws ClientProtocolException, IOException { logger.info("httpclient执行新闻资讯接口开始。");
JSONObject json = new JSONObject();
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost method = new HttpPost(url); // 设置代理
if (IS_NEED_PROXY.equals("1")) {
HttpHost proxy = new HttpHost("192.168.13.19", 7777);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
} if (list != null && list.size() > 0) {
logger.info("循环处理数据列表大小list.size={}", list != null ? list.size() : 0); // 开始循环组装post请求参数,使用倒序进行处理
for (int i = list.size() - 1; i >= 0; i--) {
HongGuTan bean = list.get(i);
if (bean == null) {
continue;
}
// 验证参数
Object[] objs = { bean.getTitle(), bean.getContent(),
bean.getSourceUrl(), bean.getSourceFrom(),
bean.getImgUrls() };
if (!validateData(objs)) {
logger.info("参数验证有误。");
continue;
}
// 接收参数json列表
JSONObject jsonParam = new JSONObject();
jsonParam.put("chnl_id", "11");// 红谷滩新闻资讯,channelId 77
jsonParam.put("title", bean.getTitle());// 标题
jsonParam.put("content", bean.getContent());// 资讯内容
jsonParam.put("source_url", bean.getSourceUrl());// 资讯源地址
jsonParam.put("source_name", bean.getSourceFrom());// 来源网站名称
jsonParam.put("img_urls", bean.getImgUrls());// 采用 url,url,url 的格式进行图片的返回 StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity); //这边使用适用正常的表单提交 // List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
//pairList.add(new BasicNameValuePair("chnl_id", "11"));
//pairList.add(new BasicNameValuePair("title", bean.getTitle()));// 标题
//pairList.add(new BasicNameValuePair("content", bean.getContent()));// 资讯内容
//pairList.add(new BasicNameValuePair("source_url", bean.getSourceUrl()));// 资讯源地址
//pairList.add(new BasicNameValuePair("source_name", bean.getSourceFrom()));// 来源网站名称
//pairList.add(new BasicNameValuePair("img_urls", bean.getImgUrls()));// 采用 url,url,url 的格式进行图片的返回
//method.setEntity(new UrlEncodedFormEntity(pairList, "utf-8")); HttpResponse result = httpClient.execute(method); // 请求结束,返回结果
String resData = EntityUtils.toString(result.getEntity());
JSONObject resJson = json.parseObject(resData);
String code = resJson.get("result_code").toString(); // 对方接口请求返回结果:0成功 1失败
logger.info("请求返回结果集{'code':" + code + ",'desc':'" + resJson.get("result_desc").toString() + "'}"); if (!StringUtils.isBlank(code) && code.trim().equals("0")) {// 成功
logger.info("业务处理成功!");
} else {
logger.error("业务处理异常");
Constants.dateMap.put("lastMaxId", bean.getId());
break;
}
}
}
}

httpclient提交json参数的更多相关文章

  1. ngResource提交json数据如何带参数

    ngResource提交json数据如何带参数 直接使用ngResource和REST服务接口交互可以让程序显得简洁,前提是配置好跨域和OPTIONS请求的支持,与此同时,如果需要带些额外的参数,有两 ...

  2. js获取get方式提交的参数返回json格式数据

    /** * 获取GET提交的参数 * @return JSON格式 * @author Terry */ function getArgs(){ var args = {}; var match = ...

  3. ②HttpURLConnection通过Json参数方式提交Post请求

    之前的文章介绍过通过报文的方式HttpURLConnection提交post请求,今天介绍下通过Json参数的方法提交Post请求,先上代码 public static HttpResponse se ...

  4. mvc ajax提交数组参数(转)

    http://blog.csdn.net/lonestar555/article/details/10192595/ 在action中的参数以数组方式接收数据 一.表单方式 1.提交Form < ...

  5. MVC中用ajax提交json对象数组

    应用场景:在前端用ajax向服务器提交json对象数组,在controller的以对象数组作为函数的参数,提交的json数组直接转为服务器端的对象数组. 如: 要将json对象数组[{Id:1,Nam ...

  6. 解决ajax get post方式提交中文参数乱码问题

    最近在工作中遇到,使用ajax get方式提交中文参数的时候出现乱码,通过上网搜索,总结出比较简单的两种解决方案: 第一种,由于tomcat默认的字符集是ISO-8859-1,修改Tomcat中的se ...

  7. Spring boot中自定义Json参数解析器

    转载请注明出处... 一.介绍 用过springMVC/spring boot的都清楚,在controller层接受参数,常用的都是两种接受方式,如下 /** * 请求路径 http://127.0. ...

  8. SpringMVC接受JSON参数详解及常见错误总结我改

    SpringMVC接受JSON参数详解及常见错误总结 最近一段时间不想使用Session了,想感受一下Token这样比较安全,稳健的方式,顺便写一个统一的接口给浏览器还有APP.所以把一个练手项目的前 ...

  9. SpringMVC接受JSON参数详解及常见错误总结

    SpringMVC接受JSON参数详解及常见错误总结 SpringMVC接受JSON参数详解及常见错误总结 最近一段时间不想使用Session了,想感受一下Token这样比较安全,稳健的方式,顺便写一 ...

随机推荐

  1. Edmond_Karp算法

    核心思想:通过bfs不断在网络中寻找最短的增广路,从而求得最大流.时间复杂度O(VE^) 算法模板: int Edmond_Karp(int s,int t) { ; memset(flow,,siz ...

  2. c++ map删除元素

    typedef std::map<std::string,float> StringFloatMap; StringFloatMap col1; StringFloatMap::itera ...

  3. Palindrome Partitioning II Leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  4. HttpRuntime.Cache的使用经验

    配置文件 <appSettings>   <add key="EnableCache" value="true"/>   "/ ...

  5. QInputDialog 使用方法

    在Qt中,如果想快速生成一个对话框,可以和用户进行简单的交互,而不需要写一个新的类的时候,就要用到QInputDialog类,这个类就是专门用来建立简单对话框的,其主要能建下列几种对话框:

  6. 一、saltstack简介和安装

    系统环境:CentOS6.5 准备yum源: epel源(包含了saltstack的包).阿里源(CentOS-Base.repo) Host解析文件: # cat /etc/hosts 192.16 ...

  7. javascript判断非空

    /* *判断非空 * */ function isEmpty(val){ if(val == null)return true; if(val == undefined || val == 'unde ...

  8. 中科大各Linux发行版的更新源列表

    sudo vi /etc/apt/sources.list Raspberry Pi deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ jessie ...

  9. RTO & RPO

    作者:王文洋链接:https://www.zhihu.com/question/30753842/answer/49334210来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. shopnc编译安装IM服务器node.js

    编译安装IM服务器node.js下载地址http://www.nodejs.org/download/ 选择Source Code node-v0.12.0 #  ./configure # make ...