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 }