public static Single<Response> request(final OkHttpClient client, final Request request) {
        return Single.create(singleEmitter -> {
            final Call call = client.newCall(request);
            final AtomicBoolean completed = new AtomicBoolean(false);
            singleEmitter.setDisposable(Disposable.fromRunnable(() -> {
                if (!completed.get()) {
                    call.cancel();
                }
            }));
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull final Call call, @NonNull final IOException e) {
                    completed.set(true);
                    singleEmitter.onError(e);
                }

                @Override
                public void onResponse(@NonNull final Call call, @NonNull final Response response) {
                    completed.set(true);
                    singleEmitter.onSuccess(response);
                }
            });
        });
    }
Create a Single for running a cancellable request.

@param client  the client to use for this request
@param request the request
@return a Single containing the response or an IOException
main/src/main/java/cgeo/geocaching/utils/RxOkHttpUtils.java