java后台给指定接口发送json数据

接口方法:

public static String post(JSONObject json, String path) {
        String result = "";

        try {
            //创建httpclient对象
            CloseableHttpClient client = HttpClients.createDefault();
            //创建post方式请求对象
            HttpPost post = new HttpPost(path);
            //设置请求头
            post.setHeader("Content-Type", "appliction/json");
            post.addHeader("usercode", "123");
            post.addHeader("password", "123");
            post.addHeader("ProtocolVersion", "1.1");
            //装填参数
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "appliction/json"));
            //设置参数到请求对象中
            post.setEntity(s);
            //执行请求操作,并拿到结果(同步阻塞)
            CloseableHttpResponse httpResponse = client.execute(post);

            InputStream in = httpResponse.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                strber.append(line + "\n");
            }
            in.close();
            result = strber.toString();
            if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                result = "服务器异常";
            }

        } catch (Exception e) {
            Logger.warn("===>LangCompareHistoryJob请求异常");
            throw new RuntimeException(e);
        }

        Logger.warn("result==" + result);
        return result;
    }

主方法:使用json拼接

public static void main(String[] args) {
        
    JSONObject jsonObject = null;
    StringBuilder json_expert = new StringBuilder("{'MessageHeader':{'Interface_ID':'LC_GK_ZJ','UUID':'"+uuid+"','Sender':'bei','Receiver':'langc','SendDate':'"+sendDate+"','SendTime':'"+sendTime+"'},'DATA':{'ROWS':[");
   for (int i = 0; i < rows.size(); i++) {

    if(i!=rows.size()-1){
         json_expert.append("{'ID':'" + id + "','LSWLDW_WLDWBH':'','LSWLDW_DWMC':'" +                     
      LSWLDW_DWMC + "','LSWLDW_DQBH':'北京','LSWLDW_DWLB':'04','LSWLDW_DWXZ':'11','LSWLDW_NBDW':'0','LSWLDW_NBDWBH':'','SJH':'" + SJH + "','YHZH':'" + YHZH + "','KHH':'','SZDW':'" + SZDW + "'},");
      }else {
        json_expert.append("{'ID':'" + id + "','LSWLDW_WLDWBH':'','LSWLDW_DWMC':'" + LSWLDW_DWMC + "','LSWLDW_DQBH':'北京','LSWLDW_DWLB':'04','LSWLDW_DWXZ':'11','LSWLDW_NBDW':'0','LSWLDW_NBDWBH':'','SJH':'" + SJH + "','YHZH':'" + YHZH + "','KHH':'','SZDW':'" + SZDW + "'}],'Count':'"+rows.size()+"'}}");
    }
   }
      post(jsonObject, "http://192.168.6.61/XXXX/XXXX");
}