简体   繁体   English

如何替换我对 RxJava 的 Retrofit 响应观察

[英]How to replace my Retrofit response to RxJava Observe

In my MVP architecture i have some interactor在我的 MVP 架构中,我有一些交互器

interface GetNoticeIntractor {

        interface OnFinishedListener {
            void onFinished(ArrayList<Notice> noticeArrayList, Main main, Wind wind);
            void onFailure(Throwable t);
        }
        void getNoticeArrayList(OnFinishedListener onFinishedListener);

    }

Here its Impl这里它的Impl

public class GetNoticeIntractorImpl implements MainContract.GetNoticeIntractor {
    public LatLng getloc(){
        return currentLocation;
    }
    @Override
    public void getNoticeArrayList(final OnFinishedListener onFinishedListener) {


        /** Create handle for the RetrofitInstance interface*/
        GetNoticeDataService service = RetrofitInstance.getRetrofitInstance().create(GetNoticeDataService.class);

        /** Call the method with parameter in the interface to get the notice data*/
        if(currentLocation!=null) {
            Call<NoticeList> call = service.getNoticeData(currentLocation.latitude, currentLocation.longitude);

            /**Log the URL called*/
            Log.wtf("URL Called", call.request().url() + "");

            call.enqueue(new Callback<NoticeList>() {
                @Override
                public void onResponse(Call<NoticeList> call, Response<NoticeList> response) {
                    onFinishedListener.onFinished(response.body().getNoticeArrayList(), response.body().getMain(), response.body().getWind());

                }

                @Override
                public void onFailure(Call<NoticeList> call, Throwable t) {
                    onFinishedListener.onFailure(t);
                }
            });
        }
    }

}

Which is using DataService哪个正在使用 DataService

public interface GetNoticeDataService {


    @GET("weather?appid=0194877ecdcac230396a119c01d46100")
    Call<NoticeList> getNoticeData(@Query("lat") double lat , @Query("lon") double lon );

}

Here is Rerofit base with CallAdapterFactory of RxJava这是带有 RxJava 的 CallAdapterFactory 的 Rerofit 基础

public class RetrofitInstance {
    private static Retrofit retrofit;
    private static final String BASE_URL = "http://api.openweathermap.org/data/2.5/";

    /**
     * Create an instance of Retrofit object
     * */
    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

The question is how to observe my GetNoticeIntractorImpl according to rxjava subscripton问题是如何根据rxjava subscripton观察我的GetNoticeIntractorImpl

Should i change my DataService to我应该将我的 DataService 更改为

@GET("weather?appid=0194877ecdcac230396a119c01d46100")
    Observable<NoticeList> getNoticeData(@Query("lat") double lat , @Query("lon") double lon );

Or only use Observable in my IntractorImpl或者只在我的 IntractorImpl 中使用 Observable

Observable.create(e -> {
            Call<NoticeList> call = service.getNoticeData(currentLocation.latitude, currentLocation.longitude);

            /**Log the URL called*/
            Log.wtf("URL Called", call.request().url() + "");

            call.enqueue(new Callback<NoticeList>() {
                @Override
                public void onResponse(Call<NoticeList> call, Response<NoticeList> response) {
                    onFinishedListener.onFinished(response.body().getNoticeArrayList(), response.body().getMain(), response.body().getWind());

                }

                @Override
                public void onFailure(Call<NoticeList> call, Throwable t) {
                    onFinishedListener.onFailure(t);
                }
            });

I need adwice which way to realize it, i'll be glad of any kind of help我需要建议以哪种方式实现它,我会很高兴得到任何帮助

I would suggest you to use official retrofit adapter for rxJava2我建议您为 rxJava2 使用官方改造适配器

compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0 

And then when creating your retrofit object add the rxjava adapter as below然后在创建改造对象时添加 rxjava 适配器,如下所示

retrofit2.Retrofit.Builder
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())

And finally your API interface should looks like最后你的 API 接口应该看起来像

@GET("weather?appid=0194877ecdcac230396a119c01d46100")
Observable<NoticeList> getNoticeData(@Query("lat") double lat , @Query("lon") double lon );

And the way to invoke it could be something like that调用它的方式可能是这样的

endpoints.getNoticeData(lat,long).subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread()).
  .subscribe(new Consumer<List<NoticeList>>() {
     @Override
     public void accept(@io.reactivex.annotations.NonNull final List<NoticeList> items)
      {
        //Use your response here
      }
    })
  ); 

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

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