Retrofit + Rxjava tarda mucho en invocar onError cuando falla la conexión del servidor
Estoy usando Retrofit + Rxjava para obtener una lista de entidades del servidor, según mi diseño cuando la tarea falla, primero verifica la conexión a Internet y luego verifica la conexión al servidor endoOnError
método deObservable
.
Cuando el cliente no está conectado a InternetdoOnError
se invoca en un tiempo razonable y el usuario recibe el mensaje de error, pero el problema es cuando Internet está conectado y obtengo un puerto o dominio incorrecto (para verificar el error del problema del servidor) Tarda mucho tiempo (aproximadamente 1 minuto o más) y es realmente molesto. ¿Cómo puedo reducir este tiempo y cuál es la razón?
public static boolean checkConnection(String ipOrUrl, int port) {
try {
int timeoutMs = 100;
Socket socket = new Socket();
SocketAddress soketAddress = new InetSocketAddress(ipOrUrl, port);
socket.connect(soketAddress, timeoutMs);
socket.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Cómo trato de obtener una lista de entidades: foodgetRetorfitService.getRestaurants()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
//here I'm checking the internet and server connection in the onlineTask if retrofit fail
//and if clients get Online this ( getRestaurantFromServer ) is called again.
.doOnError(error -> {
error.printStackTrace();
NetworkUtils.doOnlineTask(new OnlineTask() {
public void doWhenOnline() {
getResturantsFromServer();
}
}, true);
})
.subscribe(new Observer<List<Restaurant>>() {
@Override
public void onNext(List<Restaurant> restaurants) {
restaurantItemAdapter.updateAdapterData(restaurants);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: rxjava and retrofit error : can't get restaurant list");
e.printStackTrace();
}
});
cómodoOnlineTask
está implementado public static void doOnlineTask(OnlineTask onlineTask, boolean autoRetry, int retryTimeout) {
NetworkUtils.isOnline(autoRetry)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(error -> {
//here I check the Exception type ( I raised them when checking the connection )
if (error instanceof InternetConnectionException)
onlineTask.doWhenInternetFaild();
if (error instanceof ServerConnectionException)
onlineTask.doWhenServerFaild();
})
.retryWhen(t -> t.delay(2, TimeUnit.SECONDS))
.subscribe(result -> {
if (result.equals(NetworkStatus.CONNECTED))
onlineTask.doWhenOnline();
else {
if (result.equals(NetworkStatus.INTERNET_FAILD))
onlineTask.doWhenInternetFaild();
else if (result.equals(NetworkStatus.SERVER_FAILD))
onlineTask.doWhenInternetFaild();
}
}, error -> error.printStackTrace()
);
}
onlineTask
es solo una clase abstractaabstract public void doWhenOnline();
public void doWhenInternetFaild() {
//implemented somehow
}
public void doWhenServerFaild() {
//implemented somehow
}
Lo que probé:Supuse que era un problema de tiempo de espera, así que cambié el tiempo de espera de Retrofit conOkHttpClient
y no funcionó. También cambié los tiempos de espera establecidos por mí mismo y los reduje. no funciona.