MVP&Retrofit&Rxjava&Gson的简单例子

来源:互联网 发布:dns默认端口 编辑:程序博客网 时间:2024/05/17 18:00

这次我写了一个MVP&Retrofit&Rxjava&Gson的简单例子
我说个实话关于MVP在这种模式,我个人认为View和Model都是不变的,只有Presenter为了满足不同需求其中的逻辑代码会因此而改变。
这个例子Model其实就是Been类,一个存储数据的类,而Presenter会调用Retrofit相关类的函数做出一些逻辑判断,然后通过View暴露的接口来返回数据。

例子代码如下:
依赖

dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:25.3.1'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    testCompile 'junit:junit:4.12'    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'    compile 'io.reactivex:rxandroid:1.0.1'}
public interface View {    public void setView(String data);}
public class MainActivity extends AppCompatActivity implements android.com.myapplication.View{    private Button bt_get;    private TextView tv;    PresenterImpl presenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        presenter = new PresenterImpl(this);        bt_get=(Button)findViewById(R.id.bt_get);        tv = (TextView)findViewById(R.id.tv_show);        bt_get.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                presenter.getData();            }        });    }    @Override    public void setView(String data) {        tv.setText(data);    }}
public interface Presenter {    public void getData();}
public class PresenterImpl implements Presenter {    View view;    WeatherModel model;    public PresenterImpl(View view){        this.view = view;        model = new WeatherModel();    }    @Override    public void getData() {        AppClient.ApiStores apiStores = AppClient.retrofit().create(AppClient.ApiStores.class);        Observable<WeatherModel> observable = apiStores.getWeatherRxjava("101010100");        observable.subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Observer<WeatherModel>() {                    @Override                    public void onCompleted() {                        Log.i("wxl", "onCompleted");                    }                    @Override                    public void onError(Throwable e) {                        Log.i("wxl", "e=" + e.getMessage());                    }                    @Override                    public void onNext(WeatherModel weatherJson) {                        Log.i("wxl", "getWeatherinfo=" + weatherJson.getWeatherinfo().getCity());                        view.setView(weatherJson.getWeatherinfo().toString());                    }                });    }}
public class WeatherModel {    //weatherinfo需要对应json数据的名称,我之前随便写了个,被坑很久    private Weatherinfo weatherinfo;    public Weatherinfo getWeatherinfo() {        return weatherinfo;    }    public void setWeatherinfo(Weatherinfo weatherinfo) {        this.weatherinfo = weatherinfo;    }    //city、cityid必须对应json数据的名称,不然解析不了    public class Weatherinfo {        public String getCity() {            return city;        }        public void setCity(String city) {            this.city = city;        }        private String city;        private String cityid;        private String temp;        private String WD;        private String WS;        private String SD;        private String WSE;        private String time;        private String isRadar;        private String Radar;        private String njd;        private String qy;        //这里省略get和set方法        @Override        public String toString() {            return "Weatherinfo{" +                    "city='" + city + '\'' +                    ", cityid='" + cityid + '\'' +                    ", temp='" + temp + '\'' +                    ", WD='" + WD + '\'' +                    ", WS='" + WS + '\'' +                    ", SD='" + SD + '\'' +                    ", WSE='" + WSE + '\'' +                    ", time='" + time + '\'' +                    ", isRadar='" + isRadar + '\'' +                    ", Radar='" + Radar + '\'' +                    ", njd='" + njd + '\'' +                    ", qy='" + qy + '\'' +                    '}';        }    }}
public class AppClient {    static Retrofit mRetrofit;    public static Retrofit retrofit() {        if (mRetrofit == null) {            mRetrofit = new Retrofit.Builder()                    .baseUrl("http://www.weather.com.cn/")                    .addConverterFactory(GsonConverterFactory.create())                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                    .build();        }        return mRetrofit;    }    public interface ApiStores {        @GET("adat/sk/{cityId}.html")        Observable<WeatherModel> getWeatherRxjava(@Path("cityId") String cityId);    }}

其实比较完成的工程的MVP的代码要比这复杂得多,因为它们还需要在对Retrofit进行数据请求的同时不仅要做到监听还要在需要的时候去终止这个过程,其中Presenter的代码最为复杂,他承担了逻辑运算的 部分,最为容易变化。不多说了,咋也得多学习。

原创粉丝点击