简体   繁体   English

如何“返回”值(清单 <Myobject> )使用RxJava从Api调用到主线程?

[英]how to “return” value(List<Myobject>) from Api call to main thread using RxJava?

this is my ApiService interface: 这是我的ApiService接口:

 @GET("dianosticos")
Observable<List<Diagnostico>> getDiagnosticos(@Query("nombre") String patron);

this is my ApiUtil 这是我的ApiUtil

 RxJava2CallAdapterFactory rxAdapter = RxJava2CallAdapterFactory.create();

        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(rxAdapter)
                .build();

this is my attempt: 这是我的尝试:

public List<Diagnostico> getDiagnoticos(String patron){
    APIService api= ApiUtils.getAPIServiceDos();
    api.getDiagnosticos(patron).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<List<Diagnostico>>() {
                       // from here all this methods are underlined as error
                @Override
                public void onSubscribe(Subscription s) {

                }

                @Override
                public void onNext(List<Diagnostico> diagnosticos) {

                }

                @Override
                public void onError(Throwable t) {

                }

                @Override
                public void onComplete() {

                }
// up to here all underlined
            })
}

i need the list and put it in eg to use in adapter or sum in other method so i need to return a list to main thread.. is my code OK? 我需要列表并将其放入例如用于适配器或以其他方法求和,因此我需要将列表返回到主线程..我的代码可以吗? or should i do it differently way. 还是我应该以不同的方式做。 please show me how to return this list. 请告诉我如何返回此列表。 thanks in advance 提前致谢

In RxJava 2, the "subscriber" for the Observable class is Observer . 在RxJava 2中, Observable类的“订阅者”是Observer This is because the Subscriber interface is used by the Reactive Streams standard which applies to Flowable objects. 这是因为适用于Flowable对象的Reactive Streams标准使用了Subscriber接口。

This means you should replace your Subscriber object with one of the Observable classes. 这意味着您应该用Observable类之一替换Subscriber对象。 DisposableObserver is recommended because it takes care of the underlying Subscription and protocol requirements. 建议使用DisposableObserver ,因为它可以处理基本的Subscription和协议要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM