Adding a dependency preparation base class inherits rxappcompatactivity in Baseactivity To write a lifecycleprovider in the basepresenter. To facilitate the Retrofitservice setting of a manual shutdown subscription use MVP to create a new abstract class Baseview prepare interface class and request parameter interface classes New interface address class constant new request parameter interface Class Retrofitservice prepare Retrofit tool class new Retrofithelper new DataManager start using the MVP need to create a new abstract class Expressview Create a new presenter class in Mainactivity Expresspresenter last
Add Dependencies
Compile ' com.squareup.retrofit2:retrofit:2.3.0 '
compile ' com.squareup.retrofit2:converter-gson:2.3.0
' Compile ' com.squareup.retrofit2:adapter-rxjava2:2.3.0 '
compile ' com.squareup.okhttp3:logging-interceptor : 3.8.0 '
compile ' io.reactivex.rxjava2:rxandroid:2.0.1 '
compile ' com.trello.rxlifecycle2: rxlifecycle-components:2.1.0 '
The use of rxlifecycle is because in the process of using Rxjava, when a subscription is published, the page is Finsh, the logic of the subscription is not complete, it is easy to cause a memory leak problem. Prepare base class 1. Inheriting rxappcompatactivity in Baseactivity
public class Baseactivity extends Rxappcompatactivity {
}
2. Write the Lifecycleprovider method in Basepresenter to facilitate the retrofitservice settings in the rear to manually close the subscription.
public class Basepresenter {
private lifecycleprovider<activityevent> provider;
Public Basepresenter (lifecycleprovider<activityevent> provider) {
This.provider = provider;
}
Public lifecycleprovider<activityevent> Getprovider () {return
provider;
}
}
When we initialize the presenter in the activity, because of the rxappcompatactivity of the activity inheritance, we just need to preach this to pass the Lifecycleprovider over. 3. Using MVP, you need to create a new abstract class Baseview
Public interface Baseview {
/**
* Shows loading
/void Showprogressdialog ();
/**
* Hidden loading
/void Hideprogressdialog ();
/**
* Display error message
*
* @param msg error message
/void ShowError (String msg);
prepare interface class and request parameter interface classes
1. New interface address class constant
public class Constant {
/**
* Server address (base class address) */public
static final String Server_url = "http:// www.kuaidi100.com/";
/**
* Interface Request Address
*
/public static class Urlorigin {
//------------------------------------------------ --
//stitching the tail address are written below
/**
* Get the EXPRESS message * * Public
static final String get_express_info = "Query";
}
}
2. New request parameter Interface class Retrofitservice
Public interface Retrofitservice {
/**
* Get courier Information
* Rx Mode
* @return observable<expressinfo>
* *
@GET (Constant.UrlOrigin.get_express_info)
observable<expressinfo> Getexpressinforx (@ QueryMap map<string,string> Map);
Note: The parameter in the @GET (Constant.UrlOrigin.get_express_info) bracket is the trailing address "query" for Constant. Prepare Retrofit Tool class 1. New Retrofithelper
Initializes the retrofit infrastructure configuration.
public class Retrofithelper {
private static retrofithelper retrofithelper;
Private Retrofitservice Retrofitservice;
public static Retrofithelper getinstance () {return
Retrofithelper = = null? retrofithelper = new Retrofithelper (): R Etrofithelper;
}
Private Retrofithelper () {
//Initialize RETROFIT
RETROFIT RETROFIT = new Retrofit.builder ()
. BaseURL ( Constant.server_url)
. Addconverterfactory (Gsonconverterfactory.create ())//JSON parsing
. Addcalladapterfactory (Rxjava2calladapterfactory.create ())//Support Rxjava
. Client (retrofitutils.getokhttpclient ())//Print request Parameters
. Build ();
Retrofitservice = Retrofit.create (Retrofitservice.class);
}
Public Retrofitservice Getretrofitservice () {return
retrofitservice;
}
}
The Retrofitservice object can be obtained by Getretrofitservice () method, and then the interface is tuned. 2. New DataManager
Data.
public class DataManager {
private static DataManager DataManager;
Private Retrofitservice Retrofitservice;
public static DataManager getinstance () {return
DataManager = = null? datamanager = new DataManager (): datamanager;< c4/>}
/**
* Initialize retrofit, get Retrofitservice *
*
private DataManager () {
Retrofitservice = Retrofithelper.getinstance (). Getretrofitservice ();
}
---------------------------------------------------------
//From below, is the request of each interface
/**
* Get the Courier information
* @return observable<expressinfo> * * Public
observable<expressinfo> Getexpressinfo (map< String,string> map) {return
Retrofitservice.getexpressinforx (map);
}
}
Initialize Retrofithelper in DataManager and get retrofitservice by Retrofithelper heavy Getretrofitservice () method.
Then make a network request in DataManager and return the JavaBean you get, such as the Getexpressinfo () method in the code above. start using 1. To use MVP, you need to create a new abstract class Expressview
Public interface Expressview extends Baseview {
/**
* Update UI
*
* @param expressinfo Express message
/
void Updateview (Expressinfo expressinfo);
}
Expressview is set in presenter and then implemented in the activity Expressview, gets JavaBean data in the callback Updateview (), and then makes data binding. 2. In the mainactivity
public class Mainactivity extends Baseactivity implements Expressview {@BindView (r.id.tv_post_info) TextView
Ostinfo;
Private ProgressDialog ProgressDialog;
Private Expresspresenter Expresspresenter;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Butterknife.bind (this);
Expresspresenter = new Expresspresenter (this, this);
ProgressDialog = new ProgressDialog (this);
Progressdialog.setmessage ("Getting Courier information ..."); @OnClick (r.id.btn_get_post_info) public void onviewclicked () {map<string,string> Map = new Hashm
Ap<> ();
Map.put ("type", "Yuantong");
Map.put ("PostID", "11111111111");
Start making data request Expresspresenter.getexpressinfo (map); /** * Get data, do data binding operation * * @param expressinfo Express message/@Override public void Updateview (Expr Essinfo ExpreSsinfo) {Tvpostinfo.settext (expressinfo.tostring ()); /** * Display the Load box */@Override public void Showprogressdialog () {progressdialog) when the * data is requested.
Show ();
/** * The hidden loading box for data loading complete/@Override public void Hideprogressdialog () {progressdialog.hide ();
/** * Display error message * @param MSG error message * * @Override public void ShowError (String msg) {
Toast.maketext (This, MSG, Toast.length_short). Show (); }
}
3. New Presenter Class Expresspresenter
public class Expresspresenter extends Basepresenter {private Expressview expressview;
Private DataManager DataManager; Public Expresspresenter (Expressview Expressview, lifecycleprovider<activityevent> provider) {Super (provider
);
This.expressview = Expressview;
DataManager = Datamanager.getinstance (); /** * Get express message/public void Getexpressinfo (map<string,string> Map) {EXPRESSVIEW.SHOWPR
Ogressdialog (); Datamanager.getexpressinfo (map). Subscribeon (Schedulers.io ())//HTTP access in child threads. Observeon ( Androidschedulers.mainthread ())//UI thread processing returns the interface. Compose (Getprovider (). <expressinfo>binduntilevent (Act
Ivityevent.destroy)//OnDestroy unsubscribe. Subscribe (new defaultobserver<expressinfo> () {//Subscribe
@Override public void OnNext (@NonNull expressinfo expressinfo) { Expressview.uPdateview (Expressinfo); @Override public void OnError (@NonNull throwable e) {E
Xpressview.showerror (E.getmessage ());
Expressview.hideprogressdialog (); @Override public void OnComplete () {Expressview.hidepr
Ogressdialog ();
}
}); }
}
Note: Rxlifecircle manual/Automatic shutdown Code is in compose (), about rxlifecircle please pay attention to my written rxlifexcircle detailed last
OK, in the future only need to add the interface address in the constant, the new request parameter in the Retrofitservice, new method in DataManager, request network, return observable.
Then in the Getexpressinfo () method in presenter, Datamanager.get the method of Datamanger request network.
This article's demo address: http://download.csdn.net/download/huchengzhiqiang/10032097