Making the address

The renderings are as follows;





mvp.gif

MVP





mvp1.png

Retrofit Retrofit is a REST client library for Android and Java developed by Square.

Android MVP+Retrofit(encapsulation)+RxJava instance, MVP and Retrofit I will not go into detail, will be written in detail, below directly on the demo!

1. Classification First, we should plan the package name and class classification, do not put the class casually, as shown below:





example.jpg

In addition to the pattern shown above, we can also put all the MVP classes under the MVP package and then write them as shown above.

2. Add dependencies and permissions

The compile 'com. Squareup. Retrofit2: retrofit: 2.1.0' compile 'IO. Reactivex: rxandroid: 1.2.1' compile 'the IO. Reactivex: rxjava: 1.1.6' compile 'com. Google. Code. Gson: gson: 2.8.0' compile 'com. Squareup. Retrofit2: converter - gson: 2.1.0' compile 'com. Squareup. Retrofit2: adapter - rxjava: 2.1.0'Copy the code
  <uses-permission android:name="android.permission.INTERNET"/>
Copy the code

3. Define Model– Entity class and URL interface Model is actually we often write entity class, generally can be directly generated in the AS GsonFormat plug-in can be. I won’t post it here

URL interface is as follows:

public interface ApiService { //baseUrl String API_SERVER_URL = "http://apistore.baidu.com/microservice/"; @get ("weather") Observable<WeatherModel> loadDataByRetrofitRxjava(@query ("citypinyin") String cityId); // @FormUrlEncoded // @POST("user/login") // Observable<WeatherModel> getlogin(@Field("oper_name") String page, @Field("oper_pwds") String rows); }Copy the code

This requires us to take a good look at Retrofit’s annotations.

5. Connection communication (encapsulated) is actually the following code, which I have encapsulated, please download to view.

     public static Retrofit retrofit() {
        if (mRetrofit == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            OkHttpClient okHttpClient = builder.build();
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(ApiService.API_SERVER_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
        }
        return mRetrofit;
    }
Copy the code

6. The View class

public interface WeatherView {

    void getWeatherSuccess(WeatherModel weatherModel);
}
Copy the code

Below is the activity:

public class MainActivity extends AppCompatActivity implements WeatherView,View.OnClickListener { private Button btn; private TextView tv_show; private EditText edt; private WeatherPresenter weatherpresenter=new WeatherPresenter(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { tv_show= (TextView) findViewById(R.id.tv_show); btn= (Button) findViewById(R.id.btn); edt= (EditText) findViewById(R.id.edt); btn.setOnClickListener(this); } @Override public void getWeatherSuccess(WeatherModel weatherModel) { tv_show.setText(" "+weatherModel.getRetData().getWeather()+" "+weatherModel.getRetData().getWD()); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn: weatherpresenter.loadDataByRetrofitRxjava11111(edt.getText().toString()); break; } } @Override protected void onDestroy() { super.onDestroy(); if (weatherpresenter! =null){ weatherpresenter.detachView(); The e (" RXJAVA ", "destroy"); }}}Copy the code

7. Presenter classes

First write a BasePresenter:

public class BasePresenter<V> { public V mvpView; protected ApiService apiStores; private CompositeSubscription mCompositeSubscription; public void attachView(V mvpView) { this.mvpView = mvpView; apiStores = ApiClient.retrofit().create(ApiService.class); } public void detachView() { this.mvpView = null; onUnsubscribe(); } //RXjava unsubscribe to avoid memory corruption public void onUnsubscribe() {if (mCompositeSubscription! = null && mCompositeSubscription.hasSubscriptions()) { mCompositeSubscription.unsubscribe(); } } public <T> void addSubscription(Observable<T> observable, Subscriber<T> subscriber) { if (mCompositeSubscription == null) { mCompositeSubscription = new CompositeSubscription(); } mCompositeSubscription.add(observable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(subscriber)); }}Copy the code

Here’s WeatherPresenter:

public class WeatherPresenter extends BasePresenter<WeatherView>{ public WeatherPresenter(WeatherView view) { attachView(view); } public void loadDataByRetrofitRxjava11111(String cityId) { addSubscription(apiStores.loadDataByRetrofitRxjava(cityId),  new Subscriber<WeatherModel>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @override public void onNext(WeatherModel WeatherModel) {// log. e(" request succeeded ","111"); mvpView.getWeatherSuccess(weatherModel); }}); }}Copy the code

}