httpclient post推送資料
阿新 • • 發佈:2019-01-04
/** * 從介面獲取資料 * @param url 伺服器介面地址 * @param json 傳入的引數 若獲取全部,此項為空 * @return 返回查詢到的資料 * @throws HttpException * @throws IOException */ public JSONObject post(String url, String json) throws HttpException, IOException{ try { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8"); StringEntity se = new StringEntity(json, Charset.forName("UTF-8")); se.setContentType("text/json"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(se); HttpResponse response = httpClient.execute(httpPost); if (response != null && response.getStatusLine().getStatusCode() == 200){ String result = EntityUtils.toString(response.getEntity(), "UTF-8"); JSONObject resultJsonObject = JSONObject.parseObject(result); return resultJsonObject; } }catch (Exception e){ e.printStackTrace(); } return null; }
//本地tomcat // private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/getCarAll";//獲取全部資料 // private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/getCar";//獲取某一條記錄 // private String url = "http://127.0.0.1:8080/Car4s-1.0-SNAPSHOT/carApi/postCarList";//推送資料 @Test public void testPostList() throws IOException, HttpException { JSONArray jsonArray = new JSONArray(); //批量推送資料 for (int i = 1; i < 3; i++) { JSONObject jsonObject = new JSONObject(); jsonObject.put("carType", "測試00"+i); //Integer modifyType 1增加 2更新 jsonObject.put("modifyType", new Integer(1)); jsonArray.add(jsonObject); } //推送一條資料 // JSONObject jsonObject = new JSONObject(); // jsonObject.put("carSize", 4); // jsonObject.put("carType", "測試3"); // jsonObject.put("keepRequirement", "無"); // jsonObject.put("keepTitle", "無"); // jsonObject.put("keepPrice", 80.0); // jsonObject.put("repairePrice", 80.0); // jsonObject.put("modifyType", new Integer(1)); // jsonArray.add(jsonObject); String string = JSONArray.toJSONString(jsonArray); JSONObject post = post(url, string); System.out.println(post); }
/** * 推送介面,支援一條或者多條資料 * @param request * @param response * @return * @throws IOException */ @RequestMapping("/postCarList") @ResponseBody public JSONObject postCarList(HttpServletRequest request, HttpServletResponse response) throws IOException { JSONObject jsonObject = new JSONObject(); request.setCharacterEncoding("UTF-8"); BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8")); String jsonStr = null; StringBuilder result = new StringBuilder(); try { while ((jsonStr = reader.readLine()) != null){ result.append(jsonStr); } }catch (Exception e){ e.printStackTrace(); } reader.close(); JSONArray jsonArray = JSONArray.parseArray(result.toString()); try { for (int i = 0; i < jsonArray.size(); i++) { JSONObject o = (JSONObject) jsonArray.get(i); Car car = new Car(); car.setCarType(o.getString("carType")); car.setLastTime(DateUtils.stringTodate(o.getString("lastTime"), null)); car.setCarSize(o.getInteger("carSize")); car.setKeepRequirement(o.getString("carType")); car.setKeepTitle(o.getString("keepTitle")); car.setKeepPrice(o.getDouble("keepPrice")); car.setRepairePrice(o.getDouble("repairePrice")); if (o.getInteger("modifyType") == null){ jsonObject.put("msg", "Post失敗,未指定修改型別"); jsonObject.put("code", -1); return jsonObject; } int modifyType = o.getInteger("modifyType"); //增加 if (modifyType == 1){ carService.save(car); } //更新 else if (modifyType == 2){ car.setCarId(o.getInteger("carId")); carService.update(car); }else { jsonObject.put("msg", "Post失敗,修改型別指定型別錯誤"); jsonObject.put("code", -1); return jsonObject; } } jsonObject.put("msg", "Post成功"); jsonObject.put("code", 1); }catch (Exception e){ e.printStackTrace(); } return jsonObject; }