Android--retrofit+rxjava

Source: Internet
Author: User

1, now responsive programming is also more and more used in the project, just the last part of our brief introduction of the retrofit, then we have to start trying to use it together, but do not understand Rxjava students can first go to see this article (very good): http://gank.io/ Post/560e15be2dca930e00da1083#toc_27

2, then we wrote on the previous project, how to add Rxjava to our network request framework, first we have to modify the service code to

Public interface Movieservice {    @GET ("top250")    observable<movieentity> Gettopmovie (@Query ("Start") int start, @Query ("Count") int count);}

The Getmovie () method in our activity is then modified to

Package Com.qianmo.retrofitdemo;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Android.widget.Toast;import Com.qianmo.retrofitdemo.entry.movieentity;import Com.qianmo.retrofitdemo.http.movieservice;import Butterknife. Bind;import Butterknife. Butterknife;import Butterknife. Onclick;import Retrofit2. Call;import Retrofit2. Callback;import Retrofit2. Response;import Retrofit2. Retrofit;import Retrofit2.adapter.rxjava.rxjavacalladapterfactory;import Retrofit2.converter.gson.gsonconverterfactory;import Rx. Subscriber;import Rx.android.schedulers.androidschedulers;import Rx.schedulers.schedulers;public Class    Mainactivity extends Appcompatactivity {@Bind (r.id.tv_show) TextView tvShow;    @Bind (r.id.btn_request) Button btnrequest;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.actIvity_main);    Butterknife.bind (this); } @OnClick ({r.id.btn_request}) public void OnClick (view view) {switch (View.getid ()) {r.id.                Btn_request:getmovie ();        Break }}//request network data private void Getmovie () {////https://api.douban.com/v2/movie/top250?start=0&count=10 target Ground  Address//String BASEURL = "https://api.douban.com/v2/movie/";//////Create Retrofit object//Retrofit Retrofit = new Retrofit.builder ()//. BASEURL (BASEURL)//. Addconverterfactory (Gsonconverterfactory.create () )//. Build ();////movieservice movieservice = retrofit.create (Movieservice.class);//call<            Movieentity> call = Movieservice.gettopmovie (0, ten);//Call.enqueue (new callback<movieentity> () {//                 @Override//public void Onresponse (call<movieentity> call, response<movieentity> Response) {// Tvshow.settext(Response.body (). GetTitle ());//}////@Override//public void OnFailure (Call<movieenti        Ty> call, Throwable t) {//Tvshow.settext (T.getmessage ());//}//});        String baseUrl = "https://api.douban.com/v2/movie/"; Retrofit Retrofit = new Retrofit.builder (). BASEURL (BASEURL). Addconverterfactory (Gsonconver        Terfactory.create ()). Addcalladapterfactory (Rxjavacalladapterfactory.create ()). build ();        Movieservice Movieservice = retrofit.create (Movieservice.class); Movieservice.gettopmovie (0, ten). Subscribeon (Schedulers.io ()). Observeon (ANDROIDSCHEDULERS.M                    Ainthread ()). Subscribe (New subscriber<movieentity> () {@Override public void oncompleted () {Toast.maketext (Mainactivity.this, "Get Top Movie completed", TOAST.L Ength_short). Show ();                    } @Override public void OnError (Throwable e) {                    Tvshow.settext (E.getmessage ()); } @Override public void OnNext (Movieentity movieentity) {TvS                    How.settext (Movieentity.gettitle ());    }                }); }}

So we can simply integrate the Rxjava.

Android--retrofit+rxjava

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.