Interface CallX<T>
-
- Type Parameters:
T- Successful response body type.
- All Superinterfaces:
retrofit2.Call<T>,java.lang.Cloneable
public interface CallX<T> extends retrofit2.Call<T>An extension ofCallthat uses Java lambdas for the callbacks.Callbacks are created using
Consumer,BiConsumer,BiFunction, which are usable with Java 8+ lambdas and also have interoperability with Kotlin.JVM
By convention, atomic callbacks are those that would shut down the client when the call is done. This is useful for JVM only platforms, because OkHttp by default uses a non-daemon thread, this will prevent the JVM from exiting until they time out. see here:Dispatcher.executorService()Android
However, for Android, default executor should be MainThreadExecutor, which is tied to the UI thread.android.os.Handlerandroid.os.LooperSo in conclusion, for Android it's better to use enqueue callbacks.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidasync(boolean isShutdownNeeded, java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable> callback)Callsasync(BiFunction)but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.voidasync(boolean isShutdownNeeded, java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)General purpose async call method with two callbacksvoidasync(java.util.function.BiFunction<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable,@NotNull java.lang.Boolean> callback)General purpose async call method.default voidasyncBody(boolean isShutdownNeeded, @NotNull java.util.function.Consumer<@Nullable T> onSuccess, @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)Callsasync(boolean, Consumer, Consumer)and returns the response body.default voidasyncBody(boolean isShutdownNeeded, java.util.function.BiConsumer<@Nullable T,@Nullable java.lang.Throwable> callback)Callsasync(boolean, BiConsumer)but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.default voidatomicAsync(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable> callback)Callsasync(BiFunction)and automatically shuts down the client.default voidatomicAsync(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)Callsasync(boolean, Consumer, Consumer)and automatically shuts down the client.default voidatomicAsyncBody(java.util.function.BiConsumer<@Nullable T,@Nullable java.lang.Throwable> callback)CallsasyncBody(boolean, BiConsumer)with true parameter and returns the response body.voidenqueue(@NotNull java.util.function.BiConsumer<retrofit2.Call<T>,retrofit2.Response<T>> onResponse, @NotNull java.util.function.BiConsumer<retrofit2.Call<T>,java.lang.Throwable> onFailure)A lambda wrapper aroundCall.enqueue(Callback)default voidenqueueAsync(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable> callback)Callsasync(BiFunction)and doesn't care whether to shut down the client or not.default voidenqueueAsync(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)Callsasync(boolean, Consumer, Consumer)and doesn't care whether to shut down the client or not.default voidenqueueAsyncBody(@NotNull java.util.function.Consumer<@NotNull T> onSuccess, @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure, @NotNull java.util.function.Supplier<@NotNull T> defaultValueSupplier)CallsenqueueAsync(Consumer, Consumer)and definitely returns a Non-Null response body, Actually if response body is null then default value provided by defaultValueSupplier is returned.default voidenqueueAsyncBody(@NotNull java.util.function.Consumer<@Nullable T> onSuccess, @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)CallsasyncBody(boolean, Consumer, Consumer)with false parameter so it doesn't care about whether to shut down client or notdefault voidenqueueAsyncBody(java.util.function.BiConsumer<@Nullable T,@Nullable java.lang.Throwable> callback)CallsasyncBody(boolean, BiConsumer)with false parameter and returns the response body.
-
-
-
Method Detail
-
enqueue
void enqueue(@NotNull @NotNull java.util.function.BiConsumer<retrofit2.Call<T>,retrofit2.Response<T>> onResponse, @NotNull @NotNull java.util.function.BiConsumer<retrofit2.Call<T>,java.lang.Throwable> onFailure)A lambda wrapper aroundCall.enqueue(Callback)- Parameters:
onResponse-Callback.onResponse(Call, Response)onFailure-Callback.onFailure(Call, Throwable)
-
async
void async(java.util.function.BiFunction<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable,@NotNull java.lang.Boolean> callback)
General purpose async call method.CallXis a type parameter that is used to show the type of the response body.- Parameters:
callback- Represents the callback function which will be called when the request is finished or when an error occurs. The callback function will be called with two parameters:- The first parameter is the generic retrofit response object.
Response - The second parameter is the throwable object.
Throwable
- If the return value is true, the client will be shutdown.
- If the return value is false, the client will not be shutdown.
- If the return value is null, the client will not be shutdown.
More info about why auto shutdown is needed:
By default, OkHttp uses non-daemon thread, this will prevent the JVM from exiting until they time out. however, if you are using OkHttp in a non-daemon thread, like Android, this is unnecessary.
- The first parameter is the generic retrofit response object.
-
async
default void async(boolean isShutdownNeeded, java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable> callback)Callsasync(BiFunction)but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.- Parameters:
isShutdownNeeded- The boolean value which determines whether to shut down the client or not.callback- The callback function.
-
atomicAsync
default void atomicAsync(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable> callback)
Callsasync(BiFunction)and automatically shuts down the client.- Parameters:
callback- The callback function.
-
enqueueAsync
default void enqueueAsync(java.util.function.BiConsumer<@Nullable retrofit2.Response<T>,@Nullable java.lang.Throwable> callback)
Callsasync(BiFunction)and doesn't care whether to shut down the client or not.- Parameters:
callback- The callback function.
-
asyncBody
default void asyncBody(boolean isShutdownNeeded, java.util.function.BiConsumer<@Nullable T,@Nullable java.lang.Throwable> callback)Callsasync(boolean, BiConsumer)but instead of determining whether to shut down the client or not based on the return value of the callback function, it will be determined by the given boolean value.However, this method only returns the response body.
This method is useful when you want to get the response body without caring about the response status code or headers.
- Parameters:
isShutdownNeeded- The boolean value which determines whether to shut down the client or not.callback- The callback function.
-
enqueueAsyncBody
default void enqueueAsyncBody(java.util.function.BiConsumer<@Nullable T,@Nullable java.lang.Throwable> callback)
CallsasyncBody(boolean, BiConsumer)with false parameter and returns the response body.This method is useful when you want to get the response body without caring about the response status code.
This method don't care whether to shut down the client or not.
- Parameters:
callback- The callback function.
-
atomicAsyncBody
default void atomicAsyncBody(java.util.function.BiConsumer<@Nullable T,@Nullable java.lang.Throwable> callback)
CallsasyncBody(boolean, BiConsumer)with true parameter and returns the response body.This method is useful when you want to get the response body without caring about the response status code.
This method will shut down the client after the callback function is called.
- Parameters:
callback- The callback function.
-
async
void async(boolean isShutdownNeeded, java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)General purpose async call method with two callbacks- Parameters:
isShutdownNeeded- The boolean value which determines whether to shut down the client or not.onSuccess- The callback function which is called when an HTTP response is received and is not canceled.onFailure- The callback function which is called when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.
-
atomicAsync
default void atomicAsync(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)
Callsasync(boolean, Consumer, Consumer)and automatically shuts down the client.- Parameters:
onSuccess- The callback function which is called when an HTTP response is received and is not canceled.onFailure- The callback function which is called when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.
-
enqueueAsync
default void enqueueAsync(java.util.function.Consumer<@Nullable retrofit2.Response<T>> onSuccess, java.util.function.Consumer<java.lang.Throwable> onFailure)
Callsasync(boolean, Consumer, Consumer)and doesn't care whether to shut down the client or not.- Parameters:
onSuccess- The callback function which is called when an HTTP response is received and is not canceled.onFailure- The callback function which is called when a network exception occurred talking to the server or
-
asyncBody
default void asyncBody(boolean isShutdownNeeded, @NotNull @NotNull java.util.function.Consumer<@Nullable T> onSuccess, @NotNull @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)Callsasync(boolean, Consumer, Consumer)and returns the response body. ** This method is useful when you want to get the response body without caring about the response status code or headers.
- Parameters:
isShutdownNeeded- The boolean value which determines whether to shut down the client or not.onSuccess- The callback function which is called when an HTTP response is received and is not canceled.onFailure- The callback function which is called when a network exception occurred talking to the server or when an unexpected exception occurred creating the request or processing the response.
-
enqueueAsyncBody
default void enqueueAsyncBody(@NotNull @NotNull java.util.function.Consumer<@Nullable T> onSuccess, @NotNull @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure)CallsasyncBody(boolean, Consumer, Consumer)with false parameter so it doesn't care about whether to shut down client or not- Parameters:
onSuccess- The callback function which is called when an HTTP response is received and is not canceled.onFailure- The callback function which is called when a network exception occurred talking to the server or
-
enqueueAsyncBody
default void enqueueAsyncBody(@NotNull @NotNull java.util.function.Consumer<@NotNull T> onSuccess, @NotNull @NotNull java.util.function.Consumer<@Nullable java.lang.Throwable> onFailure, @NotNull @NotNull java.util.function.Supplier<@NotNull T> defaultValueSupplier)CallsenqueueAsync(Consumer, Consumer)and definitely returns a Non-Null response body, Actually if response body is null then default value provided by defaultValueSupplier is returned.- Parameters:
onSuccess- The callback function which is called when an HTTP response is received and is not canceled.onFailure- The callback function which is called when a network exception occurred talking to the server ordefaultValueSupplier- a supplier for supplying a default value in case response body is null
-
-