Bonjour,
Vous le comprendrez rapidement en voyant mon code source que je ne suis pas un expert
J'aimerais que mon application exécute les deux méthodes : enregistreur(); et enregistreur2() toutes les 60 secondes à l'infini.
J'ai essayer avec timertask,boucles avec threadsleep,...j'ai à chaques fois des soucis...
(problèmes liées à IOException, fenêtre qui n'apparait jamais...)
(L'application affiche une simple fenêtre avec un Label, et parse une page HTML et renvoie les données triés (POST) vers une page PHP)
Auriez vous une idée pour moi svp ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
 
package recupFlight;
 
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
public class RecuperFly {
 
	private JFrame frame;
 
	/**
         * Launch the application.
         */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RecuperFly window = new RecuperFly();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	/**
         * Create the application.
         */
	public RecuperFly()  throws IOException  {
		initialize();
		enregistreur();
		enregistreur2();
	}
	/**
         * Initialize the contents of the frame.
         */
	private void initialize()  throws IOException {
		frame = new JFrame();
		frame.setBounds(100, 100, 688, 442);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
 
		JLabel lblNewLabel = new JLabel("Envoie des données en cours..");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 25));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(0, 0, 672, 87);
		panel.add(lblNewLabel);
	}
 
	private void enregistreur()  throws IOException {
		/**
                 * On récupère et parse la page 
                 */
		String urlz = "https://localhost/index3.html";
        Document document = Jsoup.connect(urlz).get();
        Elements links = document.select("span[id]");
		/**
                 * On va envoyer en POST le resultat 
                 */
    	URL url = new URL("https://localhost/moulin.php");
        Map<String,Object> params = new LinkedHashMap<>();
        for (Element link : links) {
 
            params.put(link.attr("id"),link.text());
 
         }
 
        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");
 
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);
 
        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
 
        for (int c; (c = in.read()) >= 0;)
            System.out.print((char)c);
	}
 
	private void enregistreur2()  throws IOException {
		/**
                 * On récupère et parse la page 
                 */
		String urlz = "https://localhost/index3.html";
        Document document = Jsoup.connect(urlz).get();
        Elements links = document.select("td[style]");
		/**
                 * On va envoyer en POST le resultat 
                 */
    	URL url = new URL("https://localhost/moulin2.php");
        Map<String,Object> params = new LinkedHashMap<>();
        int a = 1;
        for (Element link : links) {
 
            params.put(""+a+"",link.attr("style"));
            a++;
 
         }
 
        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");
 
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);
 
        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
 
        for (int c; (c = in.read()) >= 0;)
            System.out.print((char)c);
	}
	}