如何使用Retrofit发送"fire and forget"异步调用?

Chr*_*art 2 android retrofit

我想发送永远不会返回响应的异步请求(日志记录,业务事件等).这是由Retrofit支持的吗?

Jak*_*ton 11

使用空回调:

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:

apiService.someEndpoint(Callbacks.empty());
Run Code Online (Sandbox Code Playgroud)