What I am trying to do::
I am trying to catch t
As I know, there is no internet connection the RetrofitError contains a ConnectionException as the cause.
public class RetrofitErrorHandler implements ErrorHandler {
@Override
public Throwable handleError(RetrofitError cause) {
if (cause.isNetworkError()) {
if (cause.getCause() instanceof SocketTimeoutException) {
/* your code here*/
return new MyConnectionTimeoutException();
} else {
/* your code here*/
return new MyNoConnectionException();
}
} else {
//Do whatever you want to do if there is not a network error.
}
}
}
Or you can create a custom Retrofit client that checks for connectivity before executing a request and throws an exception.
public class ConnectivityCheck implements Client {
Logger log = LoggerFactory.getLogger(ConnectivityCheck.class);
public ConnectivityCheck (Client wrappedClient, NetworkConnectivityManager ncm) {
this.wrappedClient = wrappedClient;
this.ncm = ncm;
}
Client wrappedClient;
private NetworkConnectivityManager ncm;
@Override
public Response execute(Request request) throws IOException {
if (!ncm.isConnected()) {
log.debug("No connectivity %s ", request);
throw new NoConnectivityException("No connectivity");
}
return wrappedClient.execute(request);
}
}
and then use it when configuring RestAdapter
RestAdapter.Builder().setEndpoint(serverHost)
.setClient(new ConnectivityCheck(new OkHttpClient(), ...))