/** * Return true if this runtime exception was the result of a network error. * * @return true if this runtime exception was the result of a network error */ public boolean isNetworkError() { return retrofitError.isNetworkError(); }
public int getStatusCode(RetrofitError error) { if (error.isNetworkError()) { return 503; // Use another code if you'd prefer } return error.getResponse().getStatus(); }
public abstract class BaseRequestListener<T> implements RequestListener<T> { @Override public void onRequestFailure(SpiceException spiceException) { if (spiceException.getCause() instanceof RetrofitError) { RetrofitError error = (RetrofitError) spiceException.getCause(); if (!error.isNetworkError() && (error.getResponse().getStatus() == INVALID_ACCESS_TOKEN_STATUS_CODE)) { //I'm using EventBus to broadcast this event, //this eliminates need for a Context EventBus.getDefault().post(new Events.OnTokenExpiredEvent()); //You may wish to forward this error to your listeners as well, //but I don't need that, so I'm returning here. return; } } onRequestError(spiceException); } public abstract void onRequestError(SpiceException spiceException); }
public static boolean retry(Activity act, RetrofitError error, AtomicInteger retries, String uploadErrorMsg){ // this is the first half of the retry check Response r = error.getResponse(); if (r != null && r.getStatus() == STATUS_CODE_RETRY){ Log.v(TAG, "STATUS_CODE_RETRY!"); ... return true; } // this is the second half of the retry check if (error.isNetworkError()) { if (error.getCause() instanceof SocketTimeoutException) {//connection timeout check if(retries.incrementAndGet() < NUM_RETRIES){ // retry if you can return true; } else { // if you can't retry anymore retries.set(0); Log.i(TAG, act.getClass().getSimpleName() + " has no more retries " + act.getString(R.string.timeout_msg)); return false; } } else {//no connection check retries.set(0); Log.i(TAG, act.getClass().getSimpleName() + " " + act.getString(R.string.timeout_msg)+ " " + uploadErrorMsg); return false; } } else { //non network error check retries.set(0); Log.i(TAG, act.getClass().getSimpleName() + " " + act.getString(R.string.err_msg)+ " " + uploadErrorMsg); return false; } }
String errorDescription; if (cause.isNetworkError()) { errorDescription = mContext.getString(R.string.error_network); } else {