Learning RxJava < three >flatmap

Source: Internet
Author: User
Tags throwable

For security, performance, and so on, most servers will have some interfaces that need to pass tokens to return the results correctly, and token needs to be fetched from another interface, which requires two successive requests to get the data (①token->② target data).

Using FlatMap () enables this continuous request with clearer code, avoiding Callback nested structures. Code approximate form

Api.gettoken (). FlatMap (token, api.getdata (token)). Subscribeon (Schedulers.io ()). Observeon ( Androidschedulers.mainthread ()). SUBSCRIBE (Observer);

These days to do an automatic positioning, according to the longitude latitude obtained, the query API to get the location name, and then based on the location of the API to get the weather. Of course there are APIs that can be queried directly via latitude to weather conditions, where the former is chosen to simulate two-step requests to use Flatmap

The incoming S is the latitude of longitude

The call method prints out the weather conditions and temperature

1     Private voidGetmobweather (String s) {2 stoplbs ();3 4Locationapi locationapi=network.loadlocation ();5         FinalMobweatherapi mobweatherapi=Network.loadmobweather ();6 7Locationapi.getlocation ("Cvucm4utiqosrdom", s). FlatMap (NewFunc1<location, observable<mobweather>>() {8 @Override9              PublicObservable<mobweather>Call (location location ) {Tenlist<location.resultsbean> results =location.getresults (); OneString Name=results.get (0). GetName (); ALOGGER.E ("Heweather" +name); -                 returnMobweatherapi.getmobweather ("142fcfa226a92", name); -             } the}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe (NewAction1<mobweather>() { - @Override -              Public voidCall (Mobweather mobweather) { -LOGGER.E ("getmsg" +mobweather.getmsg () + "Code" +Mobweather.getretcode ()); +                 if("200". Equals (Mobweather.getretcode ())) { -list<mobweather.resultbean>resultbeen=Mobweather.getresult (); +String Weather=resultbeen.get (0). GetWeather (); AString Temp=resultbeen.get (0). Gettemperature (); atLOGGER.E ("Weather" +weather+ "temp" +temp); -                 } -             } -},NewAction1<throwable>() { - @Override -              Public voidCall (Throwable throwable) { inLOGGER.E ("Throwable" +throwable.getmessage ()); -             } to         }); +}

Used in the network

1      Public StaticLocationapi loadlocation () {2         if(locationapi==NULL){3Retrofit retrofit=NewRetrofit.builder (). BASEURL ("https://api.thinkpage.cn/v3/"). Client (okhttpclient). Addconverterfactory (Gsonconverterfactory). Addcalladapterfactory ( rxjavacalladapterfactory). Build ();4Locationapi=retrofit.create (Locationapi.class);5         }6         returnLocationapi;7     }8 9      Public StaticMobweatherapi Loadmobweather () {Ten         if(mobweatherapi==NULL){ OneRetrofit retrofit=NewRetrofit.builder (). BASEURL ("http://apicloud.mob.com/"). Client (okhttpclient). Addconverterfactory (Gsonconverterfactory). Addcalladapterfactory ( rxjavacalladapterfactory). Build (); AMobweatherapi=retrofit.create (Mobweatherapi.class); -         } -         returnMobweatherapi; the}

Locationapi

1  Public InterfaceLocationapi {2 3     //Https://api.thinkpage.cn/v3/location/search.json?key=cvucm4utiqosrdom&q=39.93:116.404 5@GET ("Location/search.json?"))6Rx. Observable<location>getlocation (@Query ("Key") String Key,7@Query ("Q") String Q8                                        );9}

Mobweatherapi

 1  public  interface   Mobweatherapi { 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"  7  @Query ("City"  8 } 

When looking for the API and wind weather to choose the return of the JSON format a bit of a problem, can not be directly generated JavaBean with Gsonformat, the solution is here, but the operation in the network is one go, temporarily can not find where the convection processing

Learning RxJava < three >flatmap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.