如何让List<Contributor>
Observable<List<Contributor>>
中的元素逐个启动,如Observable<Contributor>
。
“Contributor
”是一个自定义类。
public class MainActivity extends AppCompatActivity {
private TextView mTv;
private interface GitHubService {
@GET("repos/{owner}/{repo}/contributors")
Observable<List<Contributor>> contributors(@Path("owner") String owner, @Path("repo") String repo);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv= (TextView) findViewById(R.id.tv_content);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(new OkHttpClient())
.build();
GitHubService service=retrofit.create(GitHubService.class);
service.contributors("square","retrofit")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Contributor>>() {
@Override
public void call(List<Contributor> contributors) {
//TODO
}
});
}
}
这是我的代码。我想最终得到这样的结果:
subscribe(new Action1<Contributor>() {
@Override
public void call(Contributor contributor) {
//TODO
}
});
答案 0 :(得分:1)
您可以将flatMap
应用于Observable<List<...>>
并从列表中创建新的observable:
Observable.just(Arrays.asList("A", "B", "C"))
.flatMap(new Func1<List<String>, Observable<String>>() {
@Override
public Observable<String> call(List<String> list) {
return Observable.from(list);
}
})
.subscribe(new Action1<String>() {
@Override
public void call(String x) {
System.out.println(x);
}
});