简体   繁体   English

从与 RxJava 的集成改造中获得实时数据响应

[英]get livedata response from integrated retrofit with RxJava

i want to get Livedata response from Retrofit integrated with RxJava for call APIs.我想从与 RxJava 集成的 Retrofit 获得 Livedata 响应以进行调用 API。 I know that RxJava response is stream and could not put it in Livedata directly.So thanks for any help.我知道 RxJava 响应是流,不能直接将其放入 Livedata。所以感谢您的帮助。

Here is a sample code for integrated Retrofit with RxJava.这是一个使用 RxJava 集成 Retrofit 的示例代码。 how can i replace Observable with Livedata.如何用 Livedata 替换 Observable。

class GitHubRxService {

   private GitHubRxApi gitHubApi;

   GitHubRxService() {
       Retrofit retrofit = new Retrofit.Builder()
         .baseUrl("https://api.github.com/")
         .addConverterFactory(GsonConverterFactory.create())
         .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
         .build();

       gitHubApi = retrofit.create(GitHubRxApi.class);
   }

   Observable<String> getTopContributors(String userName) {
       return gitHubApi.listRepos(userName)
         .flatMapIterable(x -> x)
         .flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName()))
         .flatMapIterable(x -> x)
         .filter(c -> c.getContributions() > 100)
         .sorted((a, b) -> b.getContributions() - a.getContributions())
         .map(Contributor::getName)
         .distinct();
   }
}

You can do it in this way:你可以这样做:

  1. Specify MutableLiveData instace in your class在类中指定MutableLiveData实例
  2. Create method to provide it创建方法来提供它
  3. Post value to liveData when your Observable returns valueObservable返回值时将值发布到liveData
  4. Do not forget to dispose your observables不要忘记处理你的 observables
  5. After all I advice you to use Repository for it毕竟我建议你使用 Repository

Your code:您的代码:

class GitHubRxService {

    private GitHubRxApi gitHubApi;
    private MutableLiveData<String> liveData = new MutableLiveData();
    private CompositeDisposable disposables = new CompositeDisposable();
    private GitHubRxService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        gitHubApi = retrofit.create(GitHubRxApi.class);
    }

    public LiveData<String> getLiveData() {
        return liveData;
    }

    void getTopContributors(String userName) {
        Disposable d = gitHubApi.listRepos(userName)
                .flatMapIterable(x -> x)
                .flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName()))
                .flatMapIterable(x -> x)
                .filter(c -> c.getContributions() > 100)
                .sorted((a, b) -> b.getContributions() - a.getContributions())
                .map(Contributor::getName)
                .distinct()
                .subscribe(
                        result -> {
                            liveData.postValue(result);
                        },
                        e -> {
                            e.printStackTrace();
                        },
                        () -> {
                            Log.d("Done")
                        }
                );
        disposables.add(d)
    }

    public void dispose(){
        disposables.dispose();
    }

}

Have you considered having a Repository with LiveData property in it and setting its value from the call?您是否考虑过在其中包含一个带有 LiveData 属性的存储库并从调用中设置其值?

So in general, when you will be calling a function from the Repository getTopContributors - it will return liveData, and make network call.所以一般来说,当你从 Repository getTopContributors调用一个函数时 - 它会返回 liveData,并进行网络调用。 Within the network subscription - you update liveData在网络订阅内 - 您更新 liveData

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

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