I am trying to call function sleep from my Utils as the below code:
@Override
public void onError(String err) {
log("A pinger died");
if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_FAIL)) {
PingStream.this.onError(err);
return;
}
if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_ATTEMPT_RESTART) || errorHandlingMode.equals(SpeedtestConfig.ONERROR_MUST_RESTART)) {
Utils.sleep(100);
init();
}
}
and this is the full function I have:
private void init() {
if (stopASAP) return;
if (c != null) {
try {
c.close();
} catch (Throwable ignored) {
}
}
new Thread() {
public void run() {
if (pinger != null) pinger.stopASAP();
if (remainingPings <= 0) return;
try {
c = new Connection(server, connectTimeout, soTimeout, recvBuffer, sendBuffer);
if (stopASAP) {
try {
c.close();
} catch (Throwable ignored) {
}
return;
}
pinger = new Pinger(c, path) {
@Override
public boolean onPong(long ns) {
boolean r = PingStream.this.onPong(ns);
if (--remainingPings <= 0 || !r) {
onDone();
return false;
} else return true;
}
@Override
public void onError(String err) {
log("A pinger died");
if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_FAIL)) {
PingStream.this.onError(err);
return;
}
if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_ATTEMPT_RESTART) || errorHandlingMode.equals(SpeedtestConfig.ONERROR_MUST_RESTART)) {
Utils.sleep(100);
init();
}
}
};
} catch (Throwable t) {
log("A pinger failed hard");
try {
c.close();
} catch (Throwable ignored) {
}
if (errorHandlingMode.equals(SpeedtestConfig.ONERROR_MUST_RESTART)) {
Utils.sleep(100);
init();
} else onError(t.toString());
}
}
}.start();
}
I found a compile time error with Non-static 'sleep(long)' cannot be referenced from a static context
and this is the below Utils I have:
package com.live.flutter_telephony_rf_example.core.base
import java.net.URLEncoder
object Utils {
fun urlEncode(s: String?): String? {
return try {
URLEncoder.encode(s, "utf-8")
} catch (t: Throwable) {
null
}
}
fun sleep(ms: Long) {
try {
Thread.sleep(ms)
} catch (t: Throwable) {
}
}
fun sleep(ms: Long, ns: Int) {
try {
Thread.sleep(ms, ns)
} catch (t: Throwable) {
}
}
fun url_sep(url: String): String {
return if (url.contains("?")) "&" else "?"
}
}