美文网首页
RxJava2+Retrofit2的Observable的执行探

RxJava2+Retrofit2的Observable的执行探

作者: 钰大人 | 来源:发表于2019-03-27 18:39 被阅读0次

    关于Observable的调用流程探究,自解惑.

        # gradle引用
        implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
        implementation 'io.reactivex.rxjava2:rxjava:2.2.7'
        implementation 'com.squareup.retrofit2:retrofit:2.5.0'
        implementation 'com.squareup.okhttp3:okhttp:3.14.0'
        implementation 'com.squareup.okio:okio:2.2.2'
        implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
        implementation 'com.squareup.okhttp3:logging-interceptor:3.14.0'
        implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
    
    # 一般流程如下
    mOkHttpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = chain.request();
    
                            //在这里获取到request后就可以做任何事情了
                            Response response = chain.proceed(request);
                            Log.d(TAG, "intercept: url "+request.url().toString());
                            return response;
                        }
                    }
            ).build();
    
    mRetrofit = new Retrofit.Builder()
            .client(mOkHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .baseUrl(Constant.API_BASE_URL)
            .build();
    
    Single<> xx = mRetrofit.create(targetClass.class)
    xx.scribeOn(io())
    . ...
    .subscribe();
    
    # Json解析代码器 GsonConverterFactory.create()
    # 转换为Observable及真正执行的地方 GsonConverterFactory.create()
    
    Retrofit.create(targetClass.class)的时候会生成代理,其内部逻辑如下:
    
    # 重点部分是serviceMethod的生成及adapt()的最终调用
    new InvocationHandler() {
              private final Platform platform = Platform.get();
    
              @Override public Object invoke(Object proxy, Method method, 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.callAdapter.adapt(okHttpCall);
              }
            })
    
    # serviceMethod生成的地方.
    callAdapter生成位置是ServcieMethod.createCallAdapter(), 里面调用Retrofit.nextCallAdapter(),
    最终根据注解及方法名获取对应的CallAdapter, adapterFactories.get(i).get(xxx):其实现为RxJava2CallAdapterFactory.java,函数返回值为new RxJava2CallAdapter(...);
    其中的adapt(Call)返回的是目标Observable. (此处是:CallExecuteObservable)
    Observable又在subscribe的时候调用subscribeActual, 最终调用observer.onNext/Complete/Error()
    

    相关文章

      网友评论

          本文标题:RxJava2+Retrofit2的Observable的执行探

          本文链接:https://www.haomeiwen.com/subject/apetbqtx.html