2017-08-12 51 views
2

我在Android中學習RxJava。RxJava Android序列打印數據

我:

Observable<Country> getCountries(){} // returns Countries 
Observable<City> getCapital(int countryId){} // returns capital by country id 

我想:

打印用的getCountries返回的所有國名()。然後從getCapital()方法打印每個國家的資本名稱,但id = 1的國家除外。這一切應該在一個鏈中。

例如:

private Observable<Country> getCountries(){ 
    return Observable.just(
      new Country(0, "Luxemburg"), 
      new Country(1, "Netherlands"), 
      new Country(2, "Norway"), 
      new Country(3, "India"), 
      new Country(4, "Italy") 
    ); 
} 
private Observable<City> getCapital(int countryId){ 
    City city; 
    switch (countryId){ 
     case 0: city = new City("Luxembrug"); 
      break; 
     case 1: city = new City("Amsterdam"); 
      break; 
     case 2: city = new City("Oslo"); 
      break; 
     case 3: city = new City("Delhi"); 
      break; 
     case 4: city = new City("Rome"); 
      break; 
     default: 
      city = new City(""); 
      break; 
    } 
    return Observable.just(city); 
} 


getCountries() 
     .doOnNext(country -> Log.d("TAG", "Country: id="+country.getId()+" name="+country.getName())) 
     .filter(country -> country.getId()!=1) 
     .flatMap(country -> getCapital(country.getId())) 
     .subscribe(city -> Log.d("TAG", "City: "+city.getName())); 

我想:

D: Country: id=0 name=Luxemburg 
D: Country: id=1 name=Netherlands 
D: Country: id=2 name=Norway 
D: Country: id=3 name=India 
D: Country: id=4 name=Italy 
D: City: Amsterdam 
D: City: Oslo 
D: City: Delhi 
D: City: Rome 

我能得到什麼:

D: Country: id=0 name=Luxemburg 
D: Country: id=1 name=Netherlands 
D: City: Amsterdam 
D: Country: id=2 name=Norway 
D: City: Oslo 
D: Country: id=3 name=India 
D: City: Delhi 
D: Country: id=4 name=Italy 
D: City: Rome 

我怎樣才能做到這一點?

我應該首先得到所有國家,打印它,然後獲得這些國家的首都。但我不明白我怎麼能在一條鏈中做到這一點..

感謝您的任何提示!

回答

1

假如你不能改變getCountries()getCapital(int countryId)方法就可以實現這樣的:

RxJava 1X:

getCountries() 
      .toList() 
      .doOnNext(countries -> { 
       for (Country country : countries) { 
        Log.d("TAG", "Country: id=" + country.getId() + " name=" + country.getName()); 
       } 
      }) 
      .flatMap(Observable::from) 
      .filter(country -> country.getId() != 1) 
      .flatMap(new Func1<Country, Observable<City>>() { 
       @Override public Observable<City> call(Country country) { 
        return getCapital(country.getId()); 
       } 
      }) 
      .subscribe(city -> Log.d("TAG", "City: " + city.getName())); 

RxJava 2倍:

getCountries() 
      .toList() 
      .doOnSuccess(countries -> { 
       for (Country country : countries) { 
        Log.d("TAG", "Country: id=" + country.getId() + " name=" + country.getName()); 
       } 
      }) 
      .toObservable() 
      .flatMap(Observable::fromIterable) 
      .filter(country -> country.getId() != 1) 
      .flatMap(new Function<Country, ObservableSource<City>>() { 
       @Override 
       public ObservableSource<City> apply(@NonNull Country country) throws Exception { 
        return getCapital(country.getId()); 
       } 
      }) 
      .subscribe(city -> Log.d("TAG", "City: " + city.getName())); 
+0

喜!謝謝你的迴應!但沒有工作(Android Studio強調.doOnNext()紅色。是的,我不能改變方法簽名getCountries和getCapital(),我可以改變只有方法體 – researcher

+0

這給你正是你想要的,但我使用'io.reactivex:rxjava:1.3.0'我認爲你正在使用rxjava2正確? – santalu

+0

是的,我使用rxJava2。嘗試使用rxjava 1更好嗎? – researcher