Open XBigTK13X opened 2 years ago
Something like this might work, but it will need some tweaking.
public class NetworkConnectionInterceptor implements Interceptor { private Context mContext; public NetworkConnectionInterceptor(Context context) { mContext = context; } @Override public Response intercept(Chain chain) throws IOException { if (!isConnected()) { throw new NoConnectivityException(); // Throwing our custom exception 'NoConnectivityException' } Request.Builder builder = chain.request().newBuilder(); return chain.proceed(builder.build()); } public boolean isConnected(){ ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); return (netInfo != null && netInfo.isConnected()); } }
Something like this might work, but it will need some tweaking.