Retrofit + Rxjava leva muito tempo para chamar onError quando a conexão do servidor falha
Estou usando o Retrofit + Rxjava para obter uma lista de entidades do servidor, conforme o projeto, quando a tarefa falha, primeiro verifica a conexão com a Internet e depois verifica a conexão com o servidor emdoOnError
método deObservable
.
Quando o cliente não está conectado à InternetdoOnError
é chamado em um tempo razoável e o usuário recebe a mensagem de erro, mas o problema é quando a Internet está conectada e a porta ou o domínio está errado (para verificar o erro do problema no servidor) Demora muito tempo (cerca de 1 min ou mais) e é realmente irritante. Como posso reduzir esse tempo e qual o motivo?
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;
}
}
como tento obter a 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();
}
});
quãodoOnlineTask
é 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
é apenas uma classe abstrataabstract public void doWhenOnline();
public void doWhenInternetFaild() {
//implemented somehow
}
public void doWhenServerFaild() {
//implemented somehow
}
O que eu tentei:Eu acho que é problema de tempo limite, então eu mudei o tempo limite de Retrofit comOkHttpClient
e não funcionou. Também mudei os intervalos definidos por mim e os reduzi. não está funcionando.