public class InterfaceCallExample extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JButton callButton; private JTable resultTable; private DefaultTableModel tableModel; public InterfaceCallExample() { super("Interface Call Example"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); callButton = new JButton("查看数据"); callButton.addActionListener(this); panel.add(callButton); add(panel, BorderLayout.NORTH); tableModel = new DefaultTableModel(); tableModel.addColumn("id"); tableModel.addColumn("材料类型"); tableModel.addColumn("状态"); tableModel.addColumn("创建时间"); tableModel.addColumn("更改时间"); resultTable = new JTable(tableModel); add(new JScrollPane(resultTable), BorderLayout.CENTER); } public static void main(String[] args) { InterfaceCallExample example = new InterfaceCallExample(); example.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == callButton) { try { URL url = new URL("http://101.34.120.182:8081/wnapp/productType/getTypeId?category=1&status=0&type="); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String s = reader.readLine(); JSONObject jsonObject = new JSONObject(); JSONObject object = JSONObject.parseObject(s); Object o = object.get("data"); JSONObject object1 = JSONObject.parseObject(o.toString()); ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) object1.get("list"); for (Map<String,Object> map : list){ String id = map.get("id").toString(); String titleName = map.get("titleName").toString(); String status = map.get("status").toString(); String createTime = map.get("createTime").toString(); String updateTime = map.get("updateTime").toString(); ArrayList<String> strings = new ArrayList<>(); strings.add(id); strings.add(titleName); strings.add(status); strings.add(createTime); strings.add(updateTime); String[] data = strings.toString().replace("[","").replace("]","").split(","); tableModel.addRow(data); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } 改成可以根据输入的条件进行查询
时间: 2024-03-23 08:37:36 浏览: 111
要实现根据输入的条件进行查询,你需要在界面中添加输入框和按钮,并在按钮的点击事件中获取输入框中的内容,并将其作为请求参数发送给后端接口。
下面是修改后的代码示例:
```
public class InterfaceCallExample extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton callButton;
private JTable resultTable;
private DefaultTableModel tableModel;
private JTextField categoryField, statusField, typeField;
public InterfaceCallExample() {
super("Interface Call Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
callButton = new JButton("查看数据");
callButton.addActionListener(this);
panel.add(callButton);
JLabel categoryLabel = new JLabel("材料类型:");
categoryField = new JTextField(10);
JLabel statusLabel = new JLabel("状态:");
statusField = new JTextField(10);
JLabel typeLabel = new JLabel("类型:");
typeField = new JTextField(10);
panel.add(categoryLabel);
panel.add(categoryField);
panel.add(statusLabel);
panel.add(statusField);
panel.add(typeLabel);
panel.add(typeField);
add(panel, BorderLayout.NORTH);
tableModel = new DefaultTableModel();
tableModel.addColumn("id");
tableModel.addColumn("材料类型");
tableModel.addColumn("状态");
tableModel.addColumn("创建时间");
tableModel.addColumn("更改时间");
resultTable = new JTable(tableModel);
add(new JScrollPane(resultTable), BorderLayout.CENTER);
}
public static void main(String[] args) {
InterfaceCallExample example = new InterfaceCallExample();
example.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == callButton) {
try {
String category = categoryField.getText();
String status = statusField.getText();
String type = typeField.getText();
String urlStr = "http://101.34.120.182:8081/wnapp/productType/getTypeId?";
if (!category.isEmpty()) {
urlStr += "category=" + URLEncoder.encode(category, "UTF-8") + "&";
}
if (!status.isEmpty()) {
urlStr += "status=" + URLEncoder.encode(status, "UTF-8") + "&";
}
if (!type.isEmpty()) {
urlStr += "type=" + URLEncoder.encode(type, "UTF-8") + "&";
}
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String s = reader.readLine();
JSONObject jsonObject = new JSONObject();
JSONObject object = JSONObject.parseObject(s);
Object o = object.get("data");
JSONObject object1 = JSONObject.parseObject(o.toString());
ArrayList<Map<String,Object>> list = (ArrayList<Map<String,Object>>) object1.get("list");
for (Map<String,Object> map : list){
String id = map.get("id").toString();
String titleName = map.get("titleName").toString();
String statusStr = map.get("status").toString();
String createTime = map.get("createTime").toString();
String updateTime = map.get("updateTime").toString();
ArrayList<String> strings = new ArrayList<>();
strings.add(id);
strings.add(titleName);
strings.add(statusStr);
strings.add(createTime);
strings.add(updateTime);
String[] data = strings.toString().replace("[","").replace("]","").split(",");
tableModel.addRow(data);
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
```
修改后的代码中,我们添加了三个文本框和对应的标签,用于输入查询条件。在按钮的点击事件中,我们获取这些文本框中的内容,并将其作为请求参数发送给后端接口。如果某个文本框为空,则不包含对应的查询条件。注意,我们还使用了 `URLEncoder.encode()` 方法对参数进行编码,以避免传输过程中出现空格等特殊字符导致的错误。
阅读全文