Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send a "fire and forget" async call with Retrofit?

I'd like to send async requests that never return a response (logging, business events, etc). Is this supported by Retrofit?

like image 719
Chris Stewart Avatar asked Dec 02 '25 00:12

Chris Stewart


1 Answers

Use an empty Callback:

public final class Callbacks {
  private static final Callback<Object> EMPTY = new Callback<Object> {
    @Override public void success(Object value, Response response) {}
    @Override public void failure(RetrofitError error) {}
  }

  @SuppressWarnings("unchecked")
  public static <T> Callback<T> empty() {
    return (Callback<T>) EMPTY;
  }
}

And then in your code:

apiService.someEndpoint(Callbacks.empty());
like image 127
Jake Wharton Avatar answered Dec 03 '25 13:12

Jake Wharton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!