package com.speculos.myapplicationoc; import java.io.IOException; public class ThreadPing extends Thread { /* Ce ThreadPing n'est pas un "ping" à proprement parler * on réalise un call GET sur http://domain.dev/api * pour savoir si le serveur répond * */ public static Boolean isReachable; private String url; // Constructeur public ThreadPing(String url){ this.url = url; } private int delay = 3000; // temps entre chaque ping 5sec private PingListener listen = null; public void addListener(PingListener listen){ this.listen = listen; } public void run() { while(true) { try { String apiResponse = ""; try { apiResponse = APICommandes.getMethod(url + "/api"); } catch (IOException e) { e.printStackTrace(); } if (apiResponse != "") { // Si PING OK declenche l'event ping(isReachable) isReachable = true; if(listen != null) listen.ping(isReachable); } else { isReachable = false; if(listen != null) listen.ping(isReachable); } } catch (Exception e) { System.out.println("Exception:" + e.getMessage()); } try { sleep(delay); } catch (InterruptedException e) { e.printStackTrace(); } } } }