package com.fr.list; import java.sql.Connection; import java.sql.DriverManager; import java.util.ArrayList; import java.util.List; import com.fr.data.AbstractTableData; import com.fr.json.JSONArray; import com.fr.json.JSONObject; import com.fr.third.org.apache.http.HttpResponse; import com.fr.third.org.apache.http.client.methods.HttpPost; import com.fr.third.org.apache.http.entity.StringEntity; import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; import com.fr.third.org.apache.http.impl.client.HttpClientBuilder; import com.fr.third.org.apache.http.protocol.HTTP; import com.fr.third.org.apache.http.util.EntityUtils; public class ListByJsonApi extends AbstractTableData { private static final long serialVersionUID = -1L; // 列名数组,保存程序数据集所有列名 private String[] columnNames = null; // 定义程序数据集的列数量 private int columnNum = 2; // 保存查询表的实际列数量 private int colNum = 0; // 保存查询得到列值 @SuppressWarnings("rawtypes") private ArrayList valueList = null; // 构造函数,定义表结构,该表有10个数据列,列名为column#0,column#1,。。。。。。column#9 public ListByJsonApi() { columnNames = new String[columnNum]; for (int i = 0; i < columnNum; i++) { columnNames[i] = "column#" + String.valueOf(i); } } // 实现其他四个方法 public int getColumnCount() { return columnNum; } public String getColumnName(int columnIndex) { return columnNames[columnIndex]; } public int getRowCount() { init(); return valueList.size(); } public Object getValueAt(int rowIndex, int columnIndex) { init(); if (columnIndex >= colNum) { return null; } return ((Object[]) valueList.get(rowIndex))[columnIndex]; } // 准备数据 @SuppressWarnings("unchecked") public void init() { // 确保只被执行一次 if (valueList != null) { return; } try { // 拼接请求json JSONObject jo = new JSONObject(); List js = new ArrayList(); JSONObject jd = new JSONObject(); jd.put("vCode", "2021070100001"); js.add(jd); jo.put("data", js); //接口url String urlString = "http://localhost:31211/WMS/getLeatherByCode"; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(urlString); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); // 请求Header StringEntity entity = new StringEntity(jo.toString(), "UTF-8"); // 请求json httpPost.setEntity(entity); HttpResponse response; response = httpClient.execute(httpPost); // 获取返回值 String reStr = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject reJson = new JSONObject(reStr); JSONArray array = reJson.getJSONArray("data"); // data明细 // 用对象保存数据 Object[] objArray = null; for (int i = 0; i < array.length(); i++) { objArray = new Object[colNum]; objArray[0] = array.getJSONObject(i).get("尺寸").toString(); objArray[0] = array.getJSONObject(i).get("类型").toString(); // 在valueList中加入这一行数据 valueList.add(objArray); } } catch (Exception e) { e.printStackTrace(); } } // 获取数据库连接 driverName和 url 可以换成您需要的 @SuppressWarnings("unused") public Connection getConnection() { String driverName = "org.sqlite.JDBC"; String url = "jdbc:sqlite://D:\\FineReport_8.0\\WebReport\\FRDemo.db"; String username = ""; String password = ""; Connection con = null; try { Class.forName(driverName); con = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); return null; } return null; // return con; } // 释放一些资源,因为可能会有重复调用,所以需释放valueList,将上次查询的结果释放掉 public void release() throws Exception { super.release(); this.valueList = null; } }