Simple use of retrofit and Rxjava

Source: Internet
Author: User
simple use of retrofit and RxjavaRetrofit is a network request tool after two encapsulation of the base and okhttp framework, if used alone as a network request, I think there is not much difference with xutils, volley, okhttp, just volley in the method of returning the parameter after the request, This method can be directly UI update, do not need to handler notify the main thread, because it is the thread is the main threads, so for xutils and Okhttp, this is a convenient place for it. And for retrofit, one of its advantages is that it encapsulates the request, and for our development, there are dozens of HTTP requests in a project, so there is a good encapsulation request class for the simplicity of the code has a lot of help, the use of retrofit to create a new request interface, In this interface to fill in the need to access an address of the parameters, not much said, specifically as follows, take the Watercress film data Interface for example, first Retrofit and Rxjava are required to rely on retrofit for 2.0.0,rxjava of 1.0.0, which alone retrofit do not require Rxjava dependency.
dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
Testcompile ' junit:junit:4.12 '
Compile ' com.android.support:appcompat-v7:23.2.0 '
Compile ' io.reactivex:rxjava:1.1.0 '
Compile ' io.reactivex:rxandroid:1.1.0 '
Compile ' com.squareup.retrofit2:retrofit:2.0.0-beta4 '
Compile ' com.squareup.retrofit2:converter-gson:2.0.0-beta4 '
Compile ' com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4 '
Compile ' com.google.code.gson:gson:2.6.2 '
Compile ' com.jakewharton:butterknife:7.0.1 '
}
Then there is the entity class that creates the interface data, through the Watercress movie Data interface: Get entity class https://api.douban.com/v2/movie/top250?start=0&count=10
public class Moviedao {
    /**
     * count:10
     * start:0 * total:250 *
     subjects:
     * Title: Watercress film Top2
     *
    /private int count;
    private int start;
    private int total;
    Private String title;
    private list<subjectsbean> subjects;

    public int GetCount () {return
        count;
    }

Because there are too many data, here is only the part, next is to define the HTTP request interface:
Public interface Apiservice {
/**
 * https://api.douban.com/v2/movie/top250?start=0&count=10 * *
    @GET ("v2/movie/top250")
    Call<moviedao> Loadmovie (@Query ("start") int start, @Query ("Count") int count);

    @GET ("v2/movie/top250")
    observable<moviedao> Getmoviedata (@Query ("start") int start, @Query ("Count") int count);
}

Where the @get in parentheses in the request address of the domain name in the latter part of the domain name in the retrofit written in the BaseURL, the definition of the method in the @query after the expression of the parameters and parameter types, the request interface after writing, finally in the code to configure the request information:
@Override public void OnClick (View v) {RETROFIT retrofit = new Retrofit.builder (). BaseURL (BaseURL) . Addconverterfactory (Gsonconverterfactory.create ()). Addcalladapterfactory (Rxjavacalladapterfactory.cre
    Ate ()). build ();
    Apiservice Apiservice = retrofit.create (Apiservice.class); Apiservice.getmoviedata (0,10). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthrea D ()). Subscribe (New subscriber<moviedao> () {@Override public void oncom
                Pleted () {Toast.maketext (Getactivity (), Finish, Toast.length_short). Show (); @Override public void OnError (Throwable e) {} @Overr IDE public void OnNext (Moviedao Moviedao) {Toast.maketext (Getactivity (), Moviedao.get
                Title (), Toast.length_short). Show ();
         }   }); }

Where Addconverterfactory adds support for Gson parsing, Addcalladapterfactory is adding support for Rxjava, Subscribeon is performing tasks in IO threads, Observeon even back to the main thread, The oncompleted in the subscribe callback method is the completion of the request, which updates the UI

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.