如何从查找网站获取特定数据

时间:2016-03-03 13:30:41

标签: java web

我对使用java的这个网络部分完全陌生。 我想要做的是,我希望用户添加机器类型和序列,当他按下按钮时,这将从网站中提取机器到期日期,型号和国家,然后将这些数据放入txt文件自动。 我也希望隐藏浏览器,但不知道该怎么做。 我将非常感谢你的帮助。 同样我更喜欢使用JTextArea和一个垂直的滚动窗格,允许用户一次添加多台机器(让50台机器),但同样,我也不知道如何制作程序分别从这个JTextArea中读取类型和序列 这是我目前的代码

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.URI;

public class PEW_Frame extends JFrame {

private JPanel contentPane;
private JTextField type;
private JTextField serial;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PEW_Frame frame = new PEW_Frame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public PEW_Frame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    type = new JTextField();
    type.setBounds(121, 27, 86, 20);
    contentPane.add(type);
    type.setColumns(10);

    serial = new JTextField();
    serial.setBounds(121, 72, 86, 20);
    contentPane.add(serial);
    serial.setColumns(10);

    JLabel lblNewLabel = new JLabel("New label");
    lblNewLabel.setBounds(34, 30, 46, 14);
    contentPane.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("New label");
    lblNewLabel_1.setBounds(34, 75, 46, 14);
    contentPane.add(lblNewLabel_1);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Desktop d = Desktop.getDesktop();
            String getType = type.getText();
            String getSerial = serial.getText();
            //String getType1 = type1.getText();
            //String getSerial1 = serial1.getText();
            String url = "https://www-947.ibm.com/support/entry/portal/wlup?type="+getType+"&serial="+getSerial;
            //String url1 = "https://www-947.ibm.com/support/entry/portal/wlup?type="+getType1+"&serial="+getSerial1;
            try {
                if (getType.isEmpty() || getSerial.isEmpty()) {
                    throw new Exception();
                }
                d.browse(new URI(url));
                //d.browse(new URI(url1));
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null,"You must select SLA and period","Error !", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    btnNewButton.setBounds(176, 189, 89, 23);
    contentPane.add(btnNewButton);

    //type1 = new JTextField();
    //type1.setBounds(229, 27, 86, 20);
    //contentPane.add(type1);
    //type1.setColumns(10);

    //serial1 = new JTextField();
    //serial1.setBounds(229, 72, 86, 20);
    //contentPane.add(serial1);
    //serial1.setColumns(10);
}
}

1 个答案:

答案 0 :(得分:-1)

您可以从以下示例中获得一些帮助:

https://github.com/dotKom/OnlineGuru/blob/master/onlineguru/src/main/java/no/ntnu/online/onlineguru/plugin/plugins/middag/UpdateMenu.java#L40

public void run() {
    StringBuilder sb = new StringBuilder();

    // This posts the required fields to the sit.no RESTful menu api.
    try {
        // Prepare post data
        String postData = "diner="+ kantine.toLowerCase() +"&trigger=single";

        // Open connection
        java.net.URL url = new URL(URLTEXT);
        URLConnection conn = url.openConnection();

        // Write the post data
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(postData);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }

        wr.close();
        rd.close();
    } catch (IOException e) {
        logger.error("Failed to get JSON data from "+URLTEXT, e.getCause());
    }

    // Get the html content into a clean string
    String json = new String(sb);
    json = StringEscapeUtils.unescapeJava(json);
    String html = json.split(":")[1].replaceAll("^\"|\"}$", "");

    // Traverse html with Jsoup
    String menu = findMenuItems(html);

    setMenu(kantine, menu);
}