将响应返回为Flowable时,不会调用onComplete

时间:2018-04-30 13:18:32

标签: android rx-java retrofit2 rx-java2

我正在尝试使用retrofit和rxjava 2进行webservie调用。我正在探索两种不同的方法来使用RxJava2。问题是我在使用Observable时得到了响应,但它不能与Flowable一起使用。使用Flowable我尝试调试日志时不会打印日志,但它不会进入onNextonCompleteonError。只有onSubscribe被执行。

1)使用observable作为返回类型

new WebRequestManager().getContactObservable(userRequest)   
    .subscribe(new Observer<ResponseData>() {
                   @Override
                   public void onSubscribe(Disposable d) {

                   }

                   @Override
                   public void onNext(ResponseData responseData) {

                       Log.e(TAG , "data "+responseData.getStatus());
                   }

                   @Override
                   public void onError(Throwable e) {

                   }

                   @Override
                   public void onComplete() {

                       Log.e(TAG , "data complete");

                   }
               }
    );

2)使用flowable作为返回类型

new WebRequestManager().getContactFlowable(userRequest)

 .subscribe(new Subscriber<ResponseData>() {
     @Override
     public void onSubscribe(Subscription s) {

         Log.e(TAG , "contact subscription ");

     }

     @Override
     public void onNext(ResponseData responses) {

         Log.e(TAG , "contact onNext ");


     }

     @Override
     public void onError(Throwable t) {

     }

     @Override
     public void onComplete() {

         Log.e(TAG , "contact onComplete ");
     }
 });

休息联系改造api

public interface ContactApi {

    @POST(WebRequest.GET_CONTACTS)
    Flowable<ResponseData> getContactFlowable(@Body UserRequest userRequest);

    @POST(WebRequest.GET_CONTACTS)
    Observable<ResponseData> getContactObservable(@Body UserRequest userRequest);

}

致电webservice

public Flowable<ResponseData> getContactsData(UserRequest userRequest){
        return webRequest.getWebClient().create(ContactApi.class).getContacts(userRequest);


    }

    public Observable<ResponseData> getContact(UserRequest userRequest){
        return webRequest.getWebClient().create(ContactApi.class).getContact(userRequest);

    }

获取改造实例

 public static Retrofit getWebClient(){

        //if(okHttpClient == null)
            okHttpClient = new OkHttpClient.Builder()
                                    .connectTimeout(120,TimeUnit.SECONDS)
                                    .readTimeout(120,TimeUnit.SECONDS)
                                    .writeTimeout(120,TimeUnit.SECONDS)

                                    .addInterceptor(new WebRequestInterceptor("\"application/json\""))
                                    .build();


      // if(client == null)
        client = new  Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(LoganSquareConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        return client;
    }

1 个答案:

答案 0 :(得分:2)

使用Subscriber s,您必须致电request以获取项目:

new WebRequestManager().getContactFlowable(userRequest)
    .subscribe(new Subscriber<ResponseData>() {
        @Override
        public void onSubscribe(Subscription s) {
            Log.e(TAG , "contact subscription ");
            s.request(Long.MAX_VALUE); // <---------------------------------
        }

        @Override
        public void onNext(ResponseData responses) {
            Log.e(TAG , "contact onNext ");
        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onComplete() {
            Log.e(TAG , "contact onComplete ");
        }
   });

另见DisposableSubscriber及其示例。