Retrofit简介和使用
A type-safe HTTP client for Android and Java
大家用心体会反正就是和OkHttp是不一样的作用。
第一步
在gradle中增加Retrofit的引用
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
第二步
写一个你Api的interface,下面简称Api.interface
。
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
第三步
创建一个GitHubService的实例。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
第四步
调用Git的方法请求数据。
Call<List<Repo>> repos = service.listRepos("octocat");
好了,到这里Retrofit的使用流程都已经讲完了。其实使用非常简单,就这么几步。
Retrofit源代码分析
在分析源代码之前先说几个点,等会分析源代码的时候有用。
记住重要的几个类
-
Retrofit.class
:主要对外调用的类,实例化Api.interface
。 -
ServiceMethod.class
:解析Api.interface
中的注解。 -
OkHttpCall.class
:实际的网络请求实现类。
自定义Http请求接口
Retrofit中都会有一个自定义接口,就如使用例子中的GitHubService.interface
。这里写了所有你向服务器请求的接口。在源码中不会涉及,但是源码主要是对这个接口的解析实现。分析源码的时候都会围绕着这个自定义接口展开。
Java 注解 (Annotation)
注解就是你在java代码中带有@符号的东西。最常见的是@Override
,表示重写方法。在GitHubService.interface
中都是用注解的方式表示http的请求方式,设置请求参数。比如@GET
,@POST
,@PATH
,@FIELD
等。如果对Java注解不是很理解可以点击这里Java基础加强总结(一)——注解(Annotation)。
Proxy.newProxyInstance()
public static Object newProxyInstance(
ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h
)
这个方法的主要作用就是实现动态代理。第一个参数表示一个装载器,第二个参数是指明通过接口指定生成哪个对象的代理类;第三个参数是表明代理要做的事。如果不是很理解这个方法可以点击Java基础加强总结(三)——代理(Proxy)。
适配器模式
把一个类的接口变成一个你想要的接口。像一些转换头一样,把110V的插头转换成220V的这样可以方便我们使用。比如在Retrofit中的CallAdapter。如果对适配器模式不是很理解的话可以点击这里适配器模式(Adapter Pattern)- 最易懂的设计模式解析。
以上说的几个点大家都有所了解的情况下然后开始源代码的解析。
Retrofit其实就是把我们自定义的接口实例化,实现接口中的所有方法。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
第一步创建Retrofit对象
//Retrofit.class
public static final class Builder {
private final Platform platform;
private @Nullable okhttp3.Call.Factory callFactory;
private HttpUrl baseUrl;
private final List<Converter.Factory> converterFactories = new ArrayList<>();
private final List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>();
private @Nullable Executor callbackExecutor;
private boolean validateEagerly;
Builder(Platform platform) {
this.platform = platform;
}
public Builder() {
this(Platform.get());
}
.......
/**
* Create the {@link Retrofit} instance using the configured values.
* <p>
* Note: If neither {@link #client} nor {@link #callFactory} is called a default {@link
* OkHttpClient} will be created and used.
*/
public Retrofit build() {
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
callbackExecutor = platform.defaultCallbackExecutor();
}
// Make a defensive copy of the adapters and add the default Call adapter.
List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
callAdapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));
// Make a defensive copy of the converters.
List<Converter.Factory> converterFactories =
new ArrayList<>(1 + this.converterFactories.size());
// Add the built-in converter factory first. This prevents overriding its behavior but also
// ensures correct behavior when using converters that consume all types.
converterFactories.add(new BuiltInConverters());
converterFactories.addAll(this.converterFactories);
return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
}
}
Retrofit类中可以配置选项
- platform:平台信息(Android,Java8),我们使用的是Android平台
- callFactory:创建一个OkHttp的请求
- baseUrl:根地址信息
- coverterFactories:类型转换器,包括请求类型转换器和响应数据转换器
- callAdapterFactories:请求适配器,适配到OkHttp的请求
- callbackExceutor:执行请求回调器
第二步调用Retrofit.create()
创建Api.interface
public <T> T create(final Class<T> service) {
Utils.validateServiceInterface(service);
if (validateEagerly) {
eagerlyValidateMethods(service);
}
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
new InvocationHandler() {
private final Platform platform = Platform.get();
@Override public Object invoke(Object proxy, Method method, @Nullable Object[] args)
throws Throwable {
// If the method is a method from Object then defer to normal invocation.
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
if (platform.isDefaultMethod(method)) {
return platform.invokeDefaultMethod(method, service, proxy, args);
}
ServiceMethod<Object, Object> serviceMethod =
(ServiceMethod<Object, Object>) loadServiceMethod(method);
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
return serviceMethod.adapt(okHttpCall);
}
});
}
除了前面四行是检查自定义接口外其实就只有一行,返回一个Proxy.newProxyInstance()实例。至于这个方法使用和效果可以看一下上面的要点之一Proxy.newProxyInstance()
。大概可能生成的内容如下(大概的一些代码):
public class GitHubServiceProxy extends Proxy implements GitHubService{
InvocationHandler h = new InvocationHandler(){
@Override public Object invoke(Object proxy, Method method, @Nullable Object[] args)
throws Throwable {
// If the method is a method from Object then defer to normal invocation.
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
if (platform.isDefaultMethod(method)) {
return platform.invokeDefaultMethod(method, service, proxy, args);
}
ServiceMethod<Object, Object> serviceMethod =
(ServiceMethod<Object, Object>) loadServiceMethod(method);
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
return serviceMethod.adapt(okHttpCall);
}
}
Call<List<Repo>> listRepos(){
Method method = GitHubService.class.getMethod("listRepos");
return h.invoke(this,method)
}
}
你自定义接口中的方法都会调用h.invoke()
,而invoke()
方法主要是三行代码:
第一行:将自定义api接口进行解析
ServiceMethod<Object, Object> serviceMethod =
(ServiceMethod<Object, Object>) loadServiceMethod(method);
第二行:实际的网络请求实现
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
第三行:适配
return serviceMethod.callAdapter.adapt(okHttpCall);
接下来分析第一行:
ServiceMethod<?, ?> loadServiceMethod(Method method) {
ServiceMethod<?, ?> result = serviceMethodCache.get(method);
if (result != null) return result;
synchronized (serviceMethodCache) {
result = serviceMethodCache.get(method);
if (result == null) {
result = new ServiceMethod.Builder<>(this, method).build();
serviceMethodCache.put(method, result);
}
}
return result;
}
Retrofit.class
- 缓存方法(http请求)
- 创建一个ServiceMethod类
//ServiceMethod.class
static final class Builder<T, R> {
final Retrofit retrofit;
final Method method;
final Annotation[] methodAnnotations;
final Annotation[][] parameterAnnotationsArray;
final Type[] parameterTypes;
Type responseType;
boolean gotField;
boolean gotPart;
boolean gotBody;
boolean gotPath;
boolean gotQuery;
boolean gotUrl;
String httpMethod;
boolean hasBody;
boolean isFormEncoded;
boolean isMultipart;
String relativeUrl;
Headers headers;
MediaType contentType;
Set<String> relativeUrlParamNames;
ParameterHandler<?>[] parameterHandlers;
Converter<ResponseBody, T> responseConverter;
CallAdapter<T, R> callAdapter;
public ServiceMethod build() {
//创建一个Http请求,看下面的方法实现
callAdapter = createCallAdapter();
//返回的数据类型
responseType = callAdapter.responseType();
......
//返回数据的转换类型
responseConverter = createResponseConverter();
......
//方法注解解析(@GET,@POST等)
for (Annotation annotation : methodAnnotations) {
parseMethodAnnotation(annotation);
}
......
//对参数进行设置和解析(@Path("user") String user)
int parameterCount = parameterAnnotationsArray.length;
parameterHandlers = new ParameterHandler<?>[parameterCount];
for (int p = 0; p < parameterCount; p++) {
Type parameterType = parameterTypes[p];
......
Annotation[] parameterAnnotations = parameterAnnotationsArray[p];
......
parameterHandlers[p] = parseParameter(p, parameterType, parameterAnnotations);
}
return new ServiceMethod<>(this);
}
private CallAdapter<T, R> createCallAdapter() {
//获取返回的类型
Type returnType = method.getGenericReturnType();
......
Annotation[] annotations = method.getAnnotations();
//调用Retrofit中的callAdapter,方法大家可以看一下其实就是在你配置的callAdapterFactories里找到一个合适的Adapter
return (CallAdapter<T, R>) retrofit.callAdapter(returnType, annotations);
}
}
ServiceMethod.class
- 包含了对应的方法对象,参数,注解内容,请求地址,转换方式,适配器等。几乎一个请求所需要的内容都会包含在内。
- 包含了对
Api.interface
的解析
final class OkHttpCall<T> implements Call<T> {
private final ServiceMethod<T, ?> serviceMethod;
private final @Nullable
Object[] args;
private volatile boolean canceled;
@GuardedBy("this")
private @Nullable
okhttp3.Call rawCall;
@GuardedBy("this") // Either a RuntimeException, non-fatal Error, or IOException.
private @Nullable
Throwable creationFailure;
@GuardedBy("this")
private boolean executed;
OkHttpCall(ServiceMethod<T, ?> serviceMethod, @Nullable Object[] args) {
this.serviceMethod = serviceMethod;
this.args = args;
}
//通过OkHttp.Call直接生成一个Okhttp的Request
@Override
public synchronized Request request() {
okhttp3.Call call = rawCall;
if (call != null) {
return call.request();
}
......
//
return (rawCall = createRawCall()).request();
}
//异步发送OkHttp请求
@Override
public void enqueue(final Callback<T> callback) {
okhttp3.Call call;
Throwable failure;
//创建一个OkHttp的Call
call = rawCall = createRawCall();
......
//发送请求
call.enqueue(new okhttp3.Callback() {
@Override
public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse) {
Response<T> response;
//网络请求成功返回数据,对数据进行解析
response = parseResponse(rawResponse);
......
//回调数据
callback.onResponse(retrofit2.OkHttpCall.this, response);
}
@Override
public void onFailure(okhttp3.Call call, IOException e) {
//网络请求失败,回调失败数据
callFailure(e);//callback.onFailure(retrofit2.OkHttpCall.this, e);
}
});
」
//直接发送OkHttp请求
@Override
public Response<T> execute() throws IOException {
okhttp3.Call call;
synchronized (this) {
//创建一个OkHttp的Call
call = rawCall;
if (call == null) {
call = rawCall = createRawCall();
}
}
//解析返回数据,并返回数据
return parseResponse(call.execute());
}
//将参数转换成一个正真的OkHttp的Call
private okhttp3.Call createRawCall() throws IOException {
okhttp3.Call call = serviceMethod.toCall(args);
return call;
}
//解析不同的请求返回数据
Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {
ResponseBody rawBody = rawResponse.body();
int code = rawResponse.code();
if (code < 200 || code >= 300) {
//对服务器返回小于200,大于300的数据进行处理
ResponseBody bufferedBody = Utils.buffer(rawBody);
return Response.error(bufferedBody, rawResponse);
}
if (code == 204 || code == 205) {
//对服务器返回的204和205数据进行处理
rawBody.close();
return Response.success(null, rawResponse);
}
//对正常的数据进行转换处理
retrofit2.OkHttpCall.ExceptionCatchingRequestBody catchingBody = new retrofit2.OkHttpCall.ExceptionCatchingRequestBody(rawBody);
T body = serviceMethod.toResponse(catchingBody);
return Response.success(body, rawResponse);
}
}
OkHttpCall.class
- 真正的发送网络请求的类
- 对部分网络请求的数据进行处理
到这里重要的几个类的代码已经分析的差不多了,估计大部分看完还是云里雾里。我们再拿一个例子再来捋一遍。
1、创建一个Api.interface
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
2、创建一个Retrofit对象并对Api.interface
实例化
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
3、分析一:retrofit.create()
返回的是一个T
,这个T
就是GitHubService
实例出来的对象。
4、使用GitHubService.listRepos()
方法
service.listRepos("1234").enqueue(new Callback<List<Repo>>() {
@Override
public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
}
@Override
public void onFailure(Call<List<Repo>> call, Throwable t) {
}
});
4、分析二:在调用GitHubService.listRepos()
方法的时候返回的是Call<List<Repo>>
对象。这个call
是Retrofit的Call
,代码如下:
public interface Call<T> extends Cloneable {
Response<T> execute() throws IOException;
void enqueue(Callback<T> callback);
boolean isExecuted();
void cancel();
/** The original HTTP request. */
Request request();
}
5、调用Call.enqueue()
方法,其实现的类默认是ExecutorCallAdapterFactory.ExecutorCallbackCall
。看下面的代码:
//Retrofit.class——>Builder.class——>build()方法
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
callbackExecutor = platform.defaultCallbackExecutor();
}
// Make a defensive copy of the adapters and add the default Call adapter.
List<CallAdapter.Factory> adapterFactories = new ArrayList<>(this.adapterFactories);
adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));
platform. defaultCallAdapterFactory()代码就在下面
//Platform.class
CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
if (callbackExecutor != null) {
return new ExecutorCallAdapterFactory(callbackExecutor);
}
return DefaultCallAdapterFactory.INSTANCE;
}
ExecutorCallAdapterFactory中的get()
方法中实现了CallAdapter.adapt()
返回一个ExecutorCallAdapterFactory.ExecutorCallbackCall
对象。
//ExecutorCallAdapterFactory.class
@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
if (getRawType(returnType) != Call.class) {
return null;
}
final Type responseType = Utils.getCallResponseType(returnType);
return new CallAdapter<Object, Call<?>>() {
@Override public Type responseType() {
return responseType;
}
@Override public Call<Object> adapt(Call<Object> call) {
return new ExecutorCallAdapterFactory.ExecutorCallbackCall<>(callbackExecutor, call);
}
};
}
6、分析三:ExecutorCallbackCall.class
中实现了enqueue()
方法。
//
@Override
public void enqueue(final Callback<T> callback) {
if (callback == null) throw new NullPointerException("callback == null");
delegate.enqueue(new Callback<T>() {
@Override public void onResponse(Call<T> call, final Response<T> response) {
callbackExecutor.execute(new Runnable() {
@Override public void run() {
if (delegate.isCanceled()) {
// Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
callback.onFailure(ExecutorCallAdapterFactory.ExecutorCallbackCall.this, new IOException("Canceled"));
} else {
callback.onResponse(ExecutorCallAdapterFactory.ExecutorCallbackCall.this, response);
}
}
});
}
@Override public void onFailure(Call<T> call, final Throwable t) {
callbackExecutor.execute(new Runnable() {
@Override public void run() {
callback.onFailure(ExecutorCallAdapterFactory.ExecutorCallbackCall.this, t);
}
});
}
});
}
7、enqueue()
方法中的delegate
其实就是OkHttpCall.class
对象。
CallAapter.adapt()
方法中传入的参数。在源代码分析Retrofit.create().Proxy.newProxyInstance().invoke()
中就能看到:
OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
return serviceMethod.callAdapter.adapt(okHttpCall);
8、实现enqueue()
方法中的Callback
回调函数。真正回调的方法是有callbackExecutor
执行的。而默认的callbackExecutor
是MainThreadExecutor
。看如下代码:
//Retrofit.class
public static final class Builder {
private final Platform platform;
Builder(Retrofit retrofit) {
platform = Platform.get();
......
}
public Retrofit build() {
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
callbackExecutor = platform.defaultCallbackExecutor();
}
......
return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
}
}
//Platform.class
class Platform {
private static final retrofit2.Platform PLATFORM = findPlatform();
static retrofit2.Platform get() {
return PLATFORM;
}
private static retrofit2.Platform findPlatform() {
try {
Class.forName("android.os.Build");
if (Build.VERSION.SDK_INT != 0) {
return new retrofit2.Platform.Android();
}
} catch (ClassNotFoundException ignored) {
}
try {
Class.forName("java.util.Optional");
return new retrofit2.Platform.Java8();
} catch (ClassNotFoundException ignored) {
}
return new retrofit2.Platform();
}
static class Android extends retrofit2.Platform {
@Override public Executor defaultCallbackExecutor() {
return new retrofit2.Platform.Android.MainThreadExecutor();
}
@Override CallAdapter.Factory defaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
if (callbackExecutor == null) throw new AssertionError();
return new ExecutorCallAdapterFactory(callbackExecutor);
}
static class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override public void execute(Runnable r) {
handler.post(r);
}
}
}
}
9、分析一下:其实就是保证了回调回来的位置是在主线程中。
Retrofit转换器分析
public interface Converter<F, T> {
/**
* 转换的方法
*/
T convert(F value) throws IOException;
abstract class Factory {
/**
* 响应数据转换器
*/
public @Nullable Converter<ResponseBody, ?> responseBodyConverter(Type type,
Annotation[] annotations, Retrofit retrofit) {
return null;
}
/**
* 请求参数转换器
*/
public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return null;
}
}
}
一般转换器会继承Converter<F,T>.Factory类,然后去实现Factory. responseBodyConverter()方法和Factory. requestBodyConverter()方法。
拿Gson的解析为例子
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
//GsonConverterFactory.class
public final class GsonConverterFactory extends Converter.Factory {
/**
* Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
* decoding from JSON (when no charset is specified by a header) will use UTF-8.
*/
public static GsonConverterFactory create() {
return create(new Gson());
}
/**
* Create an instance using {@code gson} for conversion. Encoding to JSON and
* decoding from JSON (when no charset is specified by a header) will use UTF-8.
*/
@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public static GsonConverterFactory create(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
return new GsonConverterFactory(gson);
}
private final Gson gson;
private GsonConverterFactory(Gson gson) {
this.gson = gson;
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new GsonResponseBodyConverter<>(gson, adapter);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new GsonRequestBodyConverter<>(gson, adapter);
}
}
//GsonResponseBodyConverter.class
final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final Gson gson;
private final TypeAdapter<T> adapter;
GsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override public T convert(ResponseBody value) throws IOException {
JsonReader jsonReader = gson.newJsonReader(value.charStream());
try {
T result = adapter.read(jsonReader);
if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
throw new JsonIOException("JSON document was not fully consumed.");
}
return result;
} finally {
value.close();
}
}
}
//GsonRequestBodyConverter.class
final class GsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private static final Charset UTF_8 = Charset.forName("UTF-8");
private final Gson gson;
private final TypeAdapter<T> adapter;
GsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
adapter.write(jsonWriter, value);
jsonWriter.close();
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
}
注意点:
1、RequestBody.create()第一个参数可以放是Content-Type类型
Retrofit适配器分析
compile 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
//RxJava2CallAdapterFactory.class
public final class RxJava2CallAdapterFactory extends CallAdapter.Factory {
public static RxJava2CallAdapterFactory create() {
return new RxJava2CallAdapterFactory(null, false);
}
public static RxJava2CallAdapterFactory createAsync() {
return new RxJava2CallAdapterFactory(null, true);
}
@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public static RxJava2CallAdapterFactory createWithScheduler(Scheduler scheduler) {
if (scheduler == null) throw new NullPointerException("scheduler == null");
return new RxJava2CallAdapterFactory(scheduler, false);
}
private final @Nullable Scheduler scheduler;
private final boolean isAsync;
private RxJava2CallAdapterFactory(@Nullable Scheduler scheduler, boolean isAsync) {
this.scheduler = scheduler;
this.isAsync = isAsync;
}
@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
Class<?> rawType = getRawType(returnType);
......
return new RxJava2CallAdapter(responseType, scheduler, isAsync, isResult, isBody, isFlowable,
isSingle, isMaybe, false);
}
}
//RxJava2CallAdapter.class
final class RxJava2CallAdapter<R> implements CallAdapter<R, Object> {
private final Type responseType;
private final @Nullable Scheduler scheduler;
private final boolean isAsync;
private final boolean isResult;
private final boolean isBody;
private final boolean isFlowable;
private final boolean isSingle;
private final boolean isMaybe;
private final boolean isCompletable;
RxJava2CallAdapter(Type responseType, @Nullable Scheduler scheduler, boolean isAsync,
boolean isResult, boolean isBody, boolean isFlowable, boolean isSingle, boolean isMaybe,
boolean isCompletable) {
this.responseType = responseType;
this.scheduler = scheduler;
this.isAsync = isAsync;
this.isResult = isResult;
this.isBody = isBody;
this.isFlowable = isFlowable;
this.isSingle = isSingle;
this.isMaybe = isMaybe;
this.isCompletable = isCompletable;
}
@Override public Type responseType() {
return responseType;
}
@Override public Object adapt(Call<R> call) {
Observable<Response<R>> responseObservable = isAsync
? new CallEnqueueObservable<>(call)
: new CallExecuteObservable<>(call);
Observable<?> observable;
if (isResult) {
observable = new ResultObservable<>(responseObservable);
} else if (isBody) {
observable = new BodyObservable<>(responseObservable);
} else {
observable = responseObservable;
}
if (scheduler != null) {
observable = observable.subscribeOn(scheduler);
}
if (isFlowable) {
return observable.toFlowable(BackpressureStrategy.LATEST);
}
if (isSingle) {
return observable.singleOrError();
}
if (isMaybe) {
return observable.singleElement();
}
if (isCompletable) {
return observable.ignoreElements();
}
return observable;
}
}
我叫陆大旭。
一个懂点心理学的无聊程序员大叔。
看完文章无论有没有收获,记得打赏、关注和点赞!
网友评论