出於安全性、性能等方面的考慮,多數服務器會有一些接口須要傳入 token 才能正確返回結果,而 token 是須要從另外一個接口獲取的,這就須要使用兩步連續的請求才能獲取數據(①token -> ②目標數據)。html
使用 flatMap() 能夠用較爲清晰的代碼實現這種連續請求,避免 Callback 嵌套的結構。代碼大體形式java
api.getToken().flatMap(token -> api.getData(token)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
這幾天作一個自動定位,根據獲取的經度 緯度,查詢api獲得地點名稱,再根據地點 查詢api獲得 天氣。 固然有api能夠直接經過經緯度查詢到天氣狀況,這裏選擇前者來模擬兩步請求的狀況 來使用flatMapjson
傳入的 s 爲 經緯度api
call 方法中 打印出 天氣狀況和溫度安全
1 private void getMobWeather(String s){ 2 stopLBS(); 3 4 LocationApi locationApi=Network.Loadlocation(); 5 final MobWeatherApi mobWeatherApi=Network.LoadMobWeather(); 6 7 locationApi.getLocation("cvucm4utiqosrdom",s).flatMap(new Func1<Location, Observable<MobWeather>>() { 8 @Override 9 public Observable<MobWeather> call(Location location) { 10 List<Location.ResultsBean> results =location.getResults(); 11 String name=results.get(0).getName(); 12 Logger.e("HeWeather "+name); 13 return mobWeatherApi.getMobWeather("142fcfa226a92",name); 14 } 15 }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<MobWeather>() { 16 @Override 17 public void call(MobWeather mobWeather) { 18 Logger.e("getMsg "+mobWeather.getMsg()+" Code "+mobWeather.getRetCode()); 19 if ("200".equals(mobWeather.getRetCode())){ 20 List<MobWeather.ResultBean>resultBeen= mobWeather.getResult(); 21 String weather=resultBeen.get(0).getWeather(); 22 String temp=resultBeen.get(0).getTemperature(); 23 Logger.e("weather "+weather+" temp "+temp); 24 } 25 } 26 }, new Action1<Throwable>() { 27 @Override 28 public void call(Throwable throwable) { 29 Logger.e("throwable "+throwable.getMessage()); 30 } 31 }); 32 }
Network中使用到的服務器
1 public static LocationApi Loadlocation(){ 2 if (locationApi==null){ 3 Retrofit retrofit=new Retrofit.Builder().baseUrl("https://api.thinkpage.cn/v3/").client(okHttpClient).addConverterFactory(gsonConverterFactory).addCallAdapterFactory(rxJavaCallAdapterFactory).build(); 4 locationApi=retrofit.create(LocationApi.class); 5 } 6 return locationApi; 7 } 8 9 public static MobWeatherApi LoadMobWeather(){ 10 if (mobWeatherApi==null){ 11 Retrofit retrofit=new Retrofit.Builder().baseUrl("http://apicloud.mob.com/").client(okHttpClient).addConverterFactory(gsonConverterFactory).addCallAdapterFactory(rxJavaCallAdapterFactory).build(); 12 mobWeatherApi=retrofit.create(MobWeatherApi.class); 13 } 14 return mobWeatherApi; 15 }
LocationApidom
1 public interface LocationApi { 2 3 //https://api.thinkpage.cn/v3/location/search.json?key=cvucm4utiqosrdom&q=39.93:116.40 4 5 @GET("location/search.json?") 6 rx.Observable<Location>getLocation(@Query("key") String key, 7 @Query("q")String q 8 ); 9 }
MobWeatherApiide
1 public interface MobWeatherApi { 2 3 //http://apicloud.mob.com/v1/weather/query?key=142fcfa226a92&city=%E5%AE%BF%E6%9D%BE 4 5 @GET("v1/weather/query?") 6 rx.Observable<MobWeather>getMobWeather(@Query("key") String key, 7 @Query("city")String city); 8 }
在找api的時候 先選擇的和風天氣返回來的json 格式有點問題,沒法直接用 gsonformat 生成javabean,解決方法在此,可是Network中的操做都是一鼓作氣,暫時不知道在哪對流進行處理性能