zoukankan      html  css  js  c++  java
  • httpclient提交json参数

     1 private void httpReqUrl(List<HongGuTan> list, String url)  
     2             throws ClientProtocolException, IOException {  
     3   
     4         logger.info("httpclient执行新闻资讯接口开始。");  
     5         JSONObject json = new JSONObject();  
     6         DefaultHttpClient httpClient = new DefaultHttpClient();  
     7   
     8         HttpPost method = new HttpPost(url);  
     9   
    10         // 设置代理  
    11         if (IS_NEED_PROXY.equals("1")) {  
    12             HttpHost proxy = new HttpHost("192.168.13.19", 7777);  
    13             httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);  
    14         }  
    15   
    16         if (list != null && list.size() > 0) {  
    17             logger.info("循环处理数据列表大小list.size={}", list != null ? list.size() : 0);  
    18   
    19             // 开始循环组装post请求参数,使用倒序进行处理  
    20             for (int i = list.size() - 1; i >= 0; i--) {  
    21                 HongGuTan bean = list.get(i);  
    22                 if (bean == null) {  
    23                     continue;  
    24                 }  
    25                 // 验证参数  
    26                 Object[] objs = { bean.getTitle(), bean.getContent(),  
    27                         bean.getSourceUrl(), bean.getSourceFrom(),  
    28                         bean.getImgUrls() };  
    29                 if (!validateData(objs)) {  
    30                     logger.info("参数验证有误。");  
    31                     continue;  
    32                 }  
    33                 // 接收参数json列表  
    34                 JSONObject jsonParam = new JSONObject();  
    35                 jsonParam.put("chnl_id", "11");// 红谷滩新闻资讯,channelId 77  
    36                 jsonParam.put("title", bean.getTitle());// 标题  
    37                 jsonParam.put("content", bean.getContent());// 资讯内容  
    38                 jsonParam.put("source_url", bean.getSourceUrl());// 资讯源地址  
    39                 jsonParam.put("source_name", bean.getSourceFrom());// 来源网站名称  
    40                 jsonParam.put("img_urls", bean.getImgUrls());// 采用 url,url,url 的格式进行图片的返回  
    41                   
    42                 StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题    
    43                 entity.setContentEncoding("UTF-8");    
    44                 entity.setContentType("application/json");    
    45                 method.setEntity(entity);    
    46                   
    47                 //这边使用适用正常的表单提交   
    48   
    49 //               List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();    
    50                 //pairList.add(new BasicNameValuePair("chnl_id", "11"));   
    51                 //pairList.add(new BasicNameValuePair("title", bean.getTitle()));// 标题    
    52                 //pairList.add(new BasicNameValuePair("content", bean.getContent()));// 资讯内容    
    53                 //pairList.add(new BasicNameValuePair("source_url", bean.getSourceUrl()));// 资讯源地址    
    54                 //pairList.add(new BasicNameValuePair("source_name", bean.getSourceFrom()));// 来源网站名称    
    55                 //pairList.add(new BasicNameValuePair("img_urls", bean.getImgUrls()));// 采用 url,url,url 的格式进行图片的返回    
    56                 //method.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));   
    57                   
    58                   
    59                 HttpResponse result = httpClient.execute(method);  
    60   
    61                 // 请求结束,返回结果  
    62                 String resData = EntityUtils.toString(result.getEntity());  
    63                 JSONObject resJson = json.parseObject(resData);  
    64                 String code = resJson.get("result_code").toString(); // 对方接口请求返回结果:0成功  1失败  
    65                 logger.info("请求返回结果集{'code':" + code + ",'desc':'" + resJson.get("result_desc").toString() + "'}");  
    66   
    67                 if (!StringUtils.isBlank(code) && code.trim().equals("0")) {// 成功  
    68                     logger.info("业务处理成功!");  
    69                 } else {  
    70                     logger.error("业务处理异常");  
    71                     Constants.dateMap.put("lastMaxId", bean.getId());  
    72                     break;  
    73                 }  
    74             }  
    75         }  
    76     }  
  • 相关阅读:
    字符串通配
    最短排序
    最长回文子串
    添加回文串
    找零钱
    最优编辑
    01背包
    PHP做分页查询(查询结果也显示为分页)
    PHP 练习3:租房子
    Html5学习3(拖放、Video(视频)、Input类型(color、datetime、email、month 、number 、range 、search、Tel、time、url、week ))
  • 原文地址:https://www.cnblogs.com/wqsbk/p/5356168.html
Copyright © 2011-2022 走看看