Rxandroid learning using an interface that aggregates data

Source: Internet
Author: User

Demo data source is a free API for aggregating data, address: https://www.juhe.cn/

Complete data request with retrofit

The example is simple and nothing is more complicated than using the operator.

Is the simple network data acquisition.

Some commonly used operators can refer to the Official document description:

Reactivex/rxjava Document Chinese version

About Rxjava Introduction, I am also a novice, dare not to tell the story, to share the online spread of the Great God Blog:

Throwing things in a big line:

RxJava for Android Developers

Hi Big Head ghost hi:

Rxjava (i: Basic article)

Easy to Rxjava (ii: operator)

Rxjava (three--the benefits of reactive formula)

Rxjava (four-using responsive programming in Android)

First, Rxjava, rxandroid dependencies are introduced in the project:

Compile ' io.reactivex:rxjava:1.0.14' io.reactivex:rxandroid:1.1.0 '

Life cycle:

Compile ' com.trello:rxlifecycle:0.4.0' com.trello:rxlifecycle-components:0.4.0 '

Introducing Retrofit Dependencies

Compile ' com.squareup.retrofit2:retrofit:2.0.0' com.squareup.retrofit2:converter-gson:2.0.0' com.squareup.retrofit2:adapter-rxjava:2.0.0 '

Then you can write the code.

First look at the run:

APIs can be used to aggregate data on official website applications.

This is the basic display of the ListView, so the program steps are simple:

First, based on the JSON data, write out the entity class. ( quickly generated with Gson plugin )

Create a layout based on the data you want to display.

Third, the preparation of adapter.

Then request and return data from the network.

Create adapter based on data and bind to the ListView for display.

These are all get requests, so the notation is the same:

To create an interface:

 Public Interface Weatherapi {    @GET ("/onebox/weather/query?") )    Observable<Weather> getweatherinfo (@Query ("CityName") String phone,                                       @Query ("key ") String key);}

Create Retrofit:

public static Weatherapi Getweatherapi () {    if (Weatherapi = = null) {        Retrofit Retrofit = new Retrofit.builder () C12/>.baseurl ("http://op.juhe.cn")                . Addcalladapterfactory (Rxjavacalladapterfactory.create ())                . Addconverterfactory (Gsonconverterfactory.create ())                . Build ();        Weatherapi = Retrofit.create (Weatherapi.class);    }    return WEATHERAPI;}

To subscribe to the trigger code in activity:

Rxview.clicks (Btn_check). Throttlefirst (3, timeunit.seconds). Subscribe (NewAction1<void>() {@Override Public voidCall (Void aVoid) {Network.getweatherapi (). Getweatherinfo (Et_city_name.gettext (). ToString (), Api_key). Subscribeon (Schedulers.newthread ()). Observeon (Android Oidschedulers.mainthread ()). Subscribe (NewAction1<weather>() {@Override Public voidCall (Weather Weather) {setdispaly (Weather);            }                        }); }        });

Weather API in code, can be used directly. Because it is a free interface, everyone can apply, but aggregated data to verify the identity card.

Examples can be downloaded in git for reference.

Https://github.com/VongVia1209/RxAndroid_Demo_With_jvhe

Article Source: Reprint http://blog.csdn.net/castledrv/article/details/51333736

Rxandroid learning using an interface that aggregates data

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.