标签:
出于安全性、性能等方面的考虑,多数服务器会有一些接口需要传入 token 才能正确返回结果,而 token 是需要从另一个接口获取的,这就需要使用两步连续的请求才能获取数据(①token -> ②目标数据)。
使用 flatMap() 可以用较为清晰的代码实现这种连续请求,避免 Callback 嵌套的结构。代码大致形式
api.getToken().flatMap(token -> api.getData(token)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
这几天做一个自动定位,根据获取的经度 纬度,查询api得到地点名称,再根据地点 查询api得到 天气。 当然有api可以直接通过经纬度查询到天气情况,这里选择前者来模拟两步请求的情况 来使用flatMap
传入的 s 为 经纬度
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 }
LocationApi
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 }
MobWeatherApi
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中的操作都是一气呵成,暂时找不到在哪对流进行处理
标签:
原文地址:http://www.cnblogs.com/demon9/p/5721391.html