Projet android NFC payment - JAVA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

package com.speculos.myapplicationoc;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
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 {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url(url + "/api")
.method("GET", null)
.build();
Response response = client.newCall(request).execute();
apiResponse = response.body().toString();
} 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();
}
}
}
}