public void send() {
timer.cancel();
final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);
final RequestCallback callback = new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
int status = response.getStatusCode();
// Notify network observers about response status
connection.fireEvent(new ConnectionStatusEvent(status));
if (status == Response.SC_OK) {
getLogger().fine("Heartbeat response OK");
} else if (status == 0) {
getLogger().warning(
"Failed sending heartbeat, server is unreachable, retrying in "
+ interval + "secs.");
} else if (status >= 500) {
getLogger().warning(
"Failed sending heartbeat, see server logs, retrying in "
+ interval + "secs.");
} else if (status == Response.SC_GONE) {
connection.showSessionExpiredError(null);
// If session is expired break the loop
return;
} else {
getLogger().warning(
"Failed sending heartbeat to server. Error code: "
+ status);
}
// Don't break the loop
schedule();
}
@Override
public void onError(Request request, Throwable exception) {
getLogger().severe(
"Exception sending heartbeat: "
+ exception.getMessage());
// Notify network observers about response status
connection.fireEvent(new ConnectionStatusEvent(0));
// Don't break the loop
schedule();
}
};
rb.setCallback(callback);
try {
getLogger().fine("Sending heartbeat request...");
rb.send();
} catch (RequestException re) {
callback.onError(null, re);
}
}