MVC和MVP实现,以及真实数据交互

1.说起mvp大家肯定都知道mvc了,首先说一下两者的区别:

MVC:

Model-View-Controller

model:数据的封装和保存,控制业务逻辑

view:视图,也就是我们工程中所说的.xml

controller:业务逻辑,相当于activity和fragment

简单点来说他们三者是相互的,循环的;view发令给controller来改变model的现状,然后model将新的数据发给view来改变

MVCdemo eg: https://github.com/chenmiaohui/MVCDemo

根据服务器写的接口传的值来创建Bean:

public class realBean {


    private DataBean data;
   

    private String message;
    private Object table;
    private String code;
    private String count;

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getTable() {
        return table;
    }

    public void setTable(Object table) {
        this.table = table;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public static class DataBean {
        /**
         * PM25 : 24
         * PM10 : 60
         * WS :
         * O3 : 87
         * AQI : 55
         * TEMP :
         * areaId : C157
         * pollutant :          * AQILevel : 1
         * HUM :
         * SO2 : 18
         * areaName : 郑州市
         * WD :
         * recTime : 2017-03-07 13:00:00
         * NO2 : 24
         * CO : 0.958
         */

        private AreaObjBean areaObj;

        public AreaObjBean getAreaObj() {
            return areaObj;
        }

        public void setAreaObj(AreaObjBean areaObj) {
            this.areaObj = areaObj;
        }

        public static class AreaObjBean {
            private int PM25;
            private int PM10;
            private String WS;
            private int O3;
            private String AQI;
            private String TEMP;
            private String areaId;
            private String pollutant;
            private String AQILevel;
            private String HUM;
            private int SO2;
            private String areaName;
            private String WD;
            private String recTime;
            private int NO2;
            private double CO;

            public int getPM25() {
                return PM25;
            }

            public void setPM25(int PM25) {
                this.PM25 = PM25;
            }

            public int getPM10() {
                return PM10;
            }

            public void setPM10(int PM10) {
                this.PM10 = PM10;
            }

            public String getWS() {
                return WS;
            }

            public void setWS(String WS) {
                this.WS = WS;
            }

            public int getO3() {
                return O3;
            }

            public void setO3(int O3) {
                this.O3 = O3;
            }

            public String getAQI() {
                return AQI;
            }

            public void setAQI(String AQI) {
                this.AQI = AQI;
            }

            public String getTEMP() {
                return TEMP;
            }

            public void setTEMP(String TEMP) {
                this.TEMP = TEMP;
            }

            public String getAreaId() {
                return areaId;
            }

            public void setAreaId(String areaId) {
                this.areaId = areaId;
            }

            public String getPollutant() {
                return pollutant;
            }

            public void setPollutant(String pollutant) {
                this.pollutant = pollutant;
            }

            public String getAQILevel() {
                return AQILevel;
            }

            public void setAQILevel(String AQILevel) {
                this.AQILevel = AQILevel;
            }

            public String getHUM() {
                return HUM;
            }

            public void setHUM(String HUM) {
                this.HUM = HUM;
            }

            public int getSO2() {
                return SO2;
            }

            public void setSO2(int SO2) {
                this.SO2 = SO2;
            }

            public String getAreaName() {
                return areaName;
            }

            public void setAreaName(String areaName) {
                this.areaName = areaName;
            }

            public String getWD() {
                return WD;
            }

            public void setWD(String WD) {
                this.WD = WD;
            }

            public String getRecTime() {
                return recTime;
            }

            public void setRecTime(String recTime) {
                this.recTime = recTime;
            }

            public int getNO2() {
                return NO2;
            }

            public void setNO2(int NO2) {
                this.NO2 = NO2;
            }

            public double getCO() {
                return CO;
            }

            public void setCO(double CO) {
                this.CO = CO;
            }
        }
    }
}

model层请求网络接口:

public interface realModel {
    //接口  其实可把onrealModel写在一起  根据接口需求来写接口所需要的数据以及监听
    void getReal(onRealModel onRealModel);
}

请求网络接口分为成功、失败、加载等等;这个根据需求来写

public interface onRealModel {
    //成功
    void OnSuccess(realBean s);
    //失败
    void OnError(String e);
}

从网络接口获取数据,也就是所说的model层:

public class realModelImpl implements realModel{

    private OkHttpClient client = new OkHttpClient();
    private Context mcontext;
    @Override
    public void getReal(final onRealModel onRealModel) {

        new Thread(){
            @Override
            public void run() {
                super.run();
                // TODO OKhttp请求网络  换成  volley请求网络
                FormEncodingBuilder builder = new FormEncodingBuilder();
                builder.add("areaId","id");
                Request request = new Request.Builder()
                        .url("uri")
                        .post(builder.build())
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        onRealModel.OnError("网络请求异常...");
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        realBean realBean = gson.fromJson(result, realBean.class);
                        if (realBean!=null){
                            onRealModel.OnSuccess(realBean);
                        }else{
                            onRealModel.OnError("解析服务器出错...");
                        }
//                        Log.e("eee","-----"+response.body().string());


                    }
                });

            }
        }.start();
    }
}

view层的xml不再代码展示,可以到github下载看

controller也就是activity和fragment来展示和控制.xml

public class MainActivity extends AppCompatActivity implements onRealModel {

    private realModelImpl realModelImpl;
    private TextView aqi;
    private TextView time;
    private TextView area;
    private TextView main_pollute;
    private LinearLayout color;
    private TextView quantity;
    private fallView fall;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aqi = (TextView) findViewById(R.id.main_aqi);
        time = (TextView) findViewById(R.id.fbsj);
        area = (TextView) findViewById(R.id.main_area_name);
        main_pollute = (TextView) findViewById(R.id.main_major_pollute);
        color = (LinearLayout) findViewById(R.id.statte_color);
        quantity = (TextView) findViewById(R.id.main_air_quantity);
        fall = (fallView) findViewById(R.id.fallview);
        realModelImpl = new realModelImpl();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //把请求传送过去 回馈过来OnSuccess();
        realModelImpl.getReal(this);
    }

    @Override
    public void OnSuccess(realBean s) {
        Message msg = new Message();
        msg.what = 1;
        msg.obj = s;
        handler.sendMessage(msg);
    }

    //展示数据
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    realBean result = (realBean) msg.obj;
                    aqi.setText(result.getData().getAreaObj().getAQI());
                    time.setText(TimeUtil.getRealHour(result.getData().getAreaObj().getRecTime()));
                    area.setText(result.getData().getAreaObj().getAreaName());
                    main_pollute.setText(result.getData().getAreaObj().getPollutant());
                    color.setBackgroundResource(getPolluteColor.getAqiStateDrawable(result.getData().getAreaObj().getAQILevel()));
                    quantity.setText(getPolluteColor.getAqiState(result.getData().getAreaObj().getAQILevel()));
                    fall.setPollutant(getPolluteColor.getAqiState(result.getData().getAreaObj().getAQILevel()));
                    break;
            }
        }
    };

    @Override
    public void OnError(String e) {

    }
}

以上便完成了mvc,demo实例:https://github.com/chenmiaohui/MVCDemo

下面来说一下MVP:

Model-View-Presenter

model:业务逻辑用来操作实际的数据,包含bean和model的抽象接口降低耦合

view:视图,需要建立一个view的抽象接口,通过view的接口来实现view与presenter的交互,从而降低耦合

presenter:view和model的中间去扭,处理和用户交互的逻辑

MVPdemo eg:https://github.com/chenmiaohui/EveryDayWeather

model层:

根据服务返回的数据bean:

public class Weather {


    /**
     * error_code : 0
     * reason : successed!
     * result : {"future":{"day_20170320":{"date":"20170320","temperature":"4~13","weather":"阴转多云","weather_id":{"fa":"02","fb":"01"},"week":"星期一","wind":"东北风微风"},"day_20170321":{"date":"20170321","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期二","wind":"东北风微风"},"day_20170322":{"date":"20170322","temperature":"6~10","weather":"小雨转多云","weather_id":{"fa":"07","fb":"01"},"week":"星期三","wind":"东风微风"},"day_20170323":{"date":"20170323","temperature":"7~10","weather":"小雨","weather_id":{"fa":"07","fb":"07"},"week":"星期四","wind":"东南风微风"},"day_20170324":{"date":"20170324","temperature":"6~17","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"week":"星期五","wind":"西风微风"},"day_20170325":{"date":"20170325","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期六","wind":"东北风微风"},"day_20170326":{"date":"20170326","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期日","wind":"东北风微风"}},"sk":{"humidity":"61%","temp":"11","time":"13:03","wind_direction":"北风","wind_strength":"2"},"today":{"city":"郑州","comfort_index":"","date_y":"20170320","dressing_advice":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","dressing_index":"较冷","drying_index":"","exercise_index":"较不宜","temperature":"4~13","travel_index":"较不宜","uv_index":"最弱","wash_index":"较适宜","weather":"阴转多云","weather_id":{"fa":"02","fb":"01"},"week":"星期一","wind":"东北风微风"}}
     * resultcode : 200
     */

    private int error_code;
    private String reason;
    /**
     * future : {"day_20170320":{"date":"20170320","temperature":"4~13","weather":"阴转多云","weather_id":{"fa":"02","fb":"01"},"week":"星期一","wind":"东北风微风"},"day_20170321":{"date":"20170321","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期二","wind":"东北风微风"},"day_20170322":{"date":"20170322","temperature":"6~10","weather":"小雨转多云","weather_id":{"fa":"07","fb":"01"},"week":"星期三","wind":"东风微风"},"day_20170323":{"date":"20170323","temperature":"7~10","weather":"小雨","weather_id":{"fa":"07","fb":"07"},"week":"星期四","wind":"东南风微风"},"day_20170324":{"date":"20170324","temperature":"6~17","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"week":"星期五","wind":"西风微风"},"day_20170325":{"date":"20170325","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期六","wind":"东北风微风"},"day_20170326":{"date":"20170326","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期日","wind":"东北风微风"}}
     * sk : {"humidity":"61%","temp":"11","time":"13:03","wind_direction":"北风","wind_strength":"2"}
     * today : {"city":"郑州","comfort_index":"","date_y":"20170320","dressing_advice":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。","dressing_index":"较冷","drying_index":"","exercise_index":"较不宜","temperature":"4~13","travel_index":"较不宜","uv_index":"最弱","wash_index":"较适宜","weather":"阴转多云","weather_id":{"fa":"02","fb":"01"},"week":"星期一","wind":"东北风微风"}
     */

    private ResultBean result;
    private String resultcode;

    public int getError_code() {
        return error_code;
    }

    public void setError_code(int error_code) {
        this.error_code = error_code;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public ResultBean getResult() {
        return result;
    }

    public void setResult(ResultBean result) {
        this.result = result;
    }

    public String getResultcode() {
        return resultcode;
    }

    public void setResultcode(String resultcode) {
        this.resultcode = resultcode;
    }

    public static class ResultBean {
        /**
         * day_20170320 : {"date":"20170320","temperature":"4~13","weather":"阴转多云","weather_id":{"fa":"02","fb":"01"},"week":"星期一","wind":"东北风微风"}
         * day_20170321 : {"date":"20170321","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期二","wind":"东北风微风"}
         * day_20170322 : {"date":"20170322","temperature":"6~10","weather":"小雨转多云","weather_id":{"fa":"07","fb":"01"},"week":"星期三","wind":"东风微风"}
         * day_20170323 : {"date":"20170323","temperature":"7~10","weather":"小雨","weather_id":{"fa":"07","fb":"07"},"week":"星期四","wind":"东南风微风"}
         * day_20170324 : {"date":"20170324","temperature":"6~17","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"week":"星期五","wind":"西风微风"}
         * day_20170325 : {"date":"20170325","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期六","wind":"东北风微风"}
         * day_20170326 : {"date":"20170326","temperature":"8~15","weather":"多云转阴","weather_id":{"fa":"01","fb":"02"},"week":"星期日","wind":"东北风微风"}
         */

        private FutureBean future;
        /**
         * humidity : 61%
         * temp : 11
         * time : 13:03
         * wind_direction : 北风
         * wind_strength : 2         */

        private SkBean sk;
        /**
         * city : 郑州
         * comfort_index :
         * date_y : 20170320         * dressing_advice : 建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。
         * dressing_index : 较冷
         * drying_index :
         * exercise_index : 较不宜
         * temperature : 4~13         * travel_index : 较不宜
         * uv_index : 最弱
         * wash_index : 较适宜
         * weather : 阴转多云
         * weather_id : {"fa":"02","fb":"01"}
         * week : 星期一
         * wind : 东北风微风
         */

        private TodayBean today;

        public FutureBean getFuture() {
            return future;
        }

        public void setFuture(FutureBean future) {
            this.future = future;
        }

        public SkBean getSk() {
            return sk;
        }

        public void setSk(SkBean sk) {
            this.sk = sk;
        }

        public TodayBean getToday() {
            return today;
        }

        public void setToday(TodayBean today) {
            this.today = today;
        }

        public static class FutureBean {
            /**
             * date : 20170320
             * temperature : 4~13             * weather : 阴转多云
             * weather_id : {"fa":"02","fb":"01"}
             * week : 星期一
             * wind : 东北风微风
             */

            private Day20170320Bean day_20170320;
            /**
             * date : 20170321
             * temperature : 8~15             * weather : 多云转阴
             * weather_id : {"fa":"01","fb":"02"}
             * week : 星期二
             * wind : 东北风微风
             */

            private Day20170321Bean day_20170321;
            /**
             * date : 20170322
             * temperature : 6~10             * weather : 小雨转多云
             * weather_id : {"fa":"07","fb":"01"}
             * week : 星期三
             * wind : 东风微风
             */

            private Day20170322Bean day_20170322;
            /**
             * date : 20170323
             * temperature : 7~10             * weather : 小雨
             * weather_id : {"fa":"07","fb":"07"}
             * week : 星期四
             * wind : 东南风微风
             */

            private Day20170323Bean day_20170323;
            /**
             * date : 20170324
             * temperature : 6~17             * weather : 多云转晴
             * weather_id : {"fa":"01","fb":"00"}
             * week : 星期五
             * wind : 西风微风
             */

            private Day20170324Bean day_20170324;
            /**
             * date : 20170325
             * temperature : 8~15             * weather : 多云转阴
             * weather_id : {"fa":"01","fb":"02"}
             * week : 星期六
             * wind : 东北风微风
             */

            private Day20170325Bean day_20170325;
            /**
             * date : 20170326
             * temperature : 8~15             * weather : 多云转阴
             * weather_id : {"fa":"01","fb":"02"}
             * week : 星期日
             * wind : 东北风微风
             */

            private Day20170326Bean day_20170326;

            public Day20170320Bean getDay_20170320() {
                return day_20170320;
            }

            public void setDay_20170320(Day20170320Bean day_20170320) {
                this.day_20170320 = day_20170320;
            }

            public Day20170321Bean getDay_20170321() {
                return day_20170321;
            }

            public void setDay_20170321(Day20170321Bean day_20170321) {
                this.day_20170321 = day_20170321;
            }

            public Day20170322Bean getDay_20170322() {
                return day_20170322;
            }

            public void setDay_20170322(Day20170322Bean day_20170322) {
                this.day_20170322 = day_20170322;
            }

            public Day20170323Bean getDay_20170323() {
                return day_20170323;
            }

            public void setDay_20170323(Day20170323Bean day_20170323) {
                this.day_20170323 = day_20170323;
            }

            public Day20170324Bean getDay_20170324() {
                return day_20170324;
            }

            public void setDay_20170324(Day20170324Bean day_20170324) {
                this.day_20170324 = day_20170324;
            }

            public Day20170325Bean getDay_20170325() {
                return day_20170325;
            }

            public void setDay_20170325(Day20170325Bean day_20170325) {
                this.day_20170325 = day_20170325;
            }

            public Day20170326Bean getDay_20170326() {
                return day_20170326;
            }

            public void setDay_20170326(Day20170326Bean day_20170326) {
                this.day_20170326 = day_20170326;
            }

            public static class Day20170320Bean {
                private String date;
                private String temperature;
                private String weather;
                /**
                 * fa : 02
                 * fb : 01
                 */

                private WeatherIdBean weather_id;
                private String week;
                private String wind;

                public String getDate() {
                    return date;
                }

                public void setDate(String date) {
                    this.date = date;
                }

                public String getTemperature() {
                    return temperature;
                }

                public void setTemperature(String temperature) {
                    this.temperature = temperature;
                }

                public String getWeather() {
                    return weather;
                }

                public void setWeather(String weather) {
                    this.weather = weather;
                }

                public WeatherIdBean getWeather_id() {
                    return weather_id;
                }

                public void setWeather_id(WeatherIdBean weather_id) {
                    this.weather_id = weather_id;
                }

                public String getWeek() {
                    return week;
                }

                public void setWeek(String week) {
                    this.week = week;
                }

                public String getWind() {
                    return wind;
                }

                public void setWind(String wind) {
                    this.wind = wind;
                }

                public static class WeatherIdBean {
                    private String fa;
                    private String fb;

                    public String getFa() {
                        return fa;
                    }

                    public void setFa(String fa) {
                        this.fa = fa;
                    }

                    public String getFb() {
                        return fb;
                    }

                    public void setFb(String fb) {
                        this.fb = fb;
                    }
                }
            }

            public static class Day20170321Bean {
                private String date;
                private String temperature;
                private String weather;
                /**
                 * fa : 01
                 * fb : 02
                 */

                private WeatherIdBean weather_id;
                private String week;
                private String wind;

                public String getDate() {
                    return date;
                }

                public void setDate(String date) {
                    this.date = date;
                }

                public String getTemperature() {
                    return temperature;
                }

                public void setTemperature(String temperature) {
                    this.temperature = temperature;
                }

                public String getWeather() {
                    return weather;
                }

                public void setWeather(String weather) {
                    this.weather = weather;
                }

                public WeatherIdBean getWeather_id() {
                    return weather_id;
                }

                public void setWeather_id(WeatherIdBean weather_id) {
                    this.weather_id = weather_id;
                }

                public String getWeek() {
                    return week;
                }

                public void setWeek(String week) {
                    this.week = week;
                }

                public String getWind() {
                    return wind;
                }

                public void setWind(String wind) {
                    this.wind = wind;
                }

                public static class WeatherIdBean {
                    private String fa;
                    private String fb;

                    public String getFa() {
                        return fa;
                    }

                    public void setFa(String fa) {
                        this.fa = fa;
                    }

                    public String getFb() {
                        return fb;
                    }

                    public void setFb(String fb) {
                        this.fb = fb;
                    }
                }
            }

            public static class Day20170322Bean {
                private String date;
                private String temperature;
                private String weather;
                /**
                 * fa : 07
                 * fb : 01
                 */

                private WeatherIdBean weather_id;
                private String week;
                private String wind;

                public String getDate() {
                    return date;
                }

                public void setDate(String date) {
                    this.date = date;
                }

                public String getTemperature() {
                    return temperature;
                }

                public void setTemperature(String temperature) {
                    this.temperature = temperature;
                }

                public String getWeather() {
                    return weather;
                }

                public void setWeather(String weather) {
                    this.weather = weather;
                }

                public WeatherIdBean getWeather_id() {
                    return weather_id;
                }

                public void setWeather_id(WeatherIdBean weather_id) {
                    this.weather_id = weather_id;
                }

                public String getWeek() {
                    return week;
                }

                public void setWeek(String week) {
                    this.week = week;
                }

                public String getWind() {
                    return wind;
                }

                public void setWind(String wind) {
                    this.wind = wind;
                }

                public static class WeatherIdBean {
                    private String fa;
                    private String fb;

                    public String getFa() {
                        return fa;
                    }

                    public void setFa(String fa) {
                        this.fa = fa;
                    }

                    public String getFb() {
                        return fb;
                    }

                    public void setFb(String fb) {
                        this.fb = fb;
                    }
                }
            }

            public static class Day20170323Bean {
                private String date;
                private String temperature;
                private String weather;
                /**
                 * fa : 07
                 * fb : 07
                 */

                private WeatherIdBean weather_id;
                private String week;
                private String wind;

                public String getDate() {
                    return date;
                }

                public void setDate(String date) {
                    this.date = date;
                }

                public String getTemperature() {
                    return temperature;
                }

                public void setTemperature(String temperature) {
                    this.temperature = temperature;
                }

                public String getWeather() {
                    return weather;
                }

                public void setWeather(String weather) {
                    this.weather = weather;
                }

                public WeatherIdBean getWeather_id() {
                    return weather_id;
                }

                public void setWeather_id(WeatherIdBean weather_id) {
                    this.weather_id = weather_id;
                }

                public String getWeek() {
                    return week;
                }

                public void setWeek(String week) {
                    this.week = week;
                }

                public String getWind() {
                    return wind;
                }

                public void setWind(String wind) {
                    this.wind = wind;
                }

                public static class WeatherIdBean {
                    private String fa;
                    private String fb;

                    public String getFa() {
                        return fa;
                    }

                    public void setFa(String fa) {
                        this.fa = fa;
                    }

                    public String getFb() {
                        return fb;
                    }

                    public void setFb(String fb) {
                        this.fb = fb;
                    }
                }
            }

            public static class Day20170324Bean {
                private String date;
                private String temperature;
                private String weather;
                /**
                 * fa : 01
                 * fb : 00
                 */

                private WeatherIdBean weather_id;
                private String week;
                private String wind;

                public String getDate() {
                    return date;
                }

                public void setDate(String date) {
                    this.date = date;
                }

                public String getTemperature() {
                    return temperature;
                }

                public void setTemperature(String temperature) {
                    this.temperature = temperature;
                }

                public String getWeather() {
                    return weather;
                }

                public void setWeather(String weather) {
                    this.weather = weather;
                }

                public WeatherIdBean getWeather_id() {
                    return weather_id;
                }

                public void setWeather_id(WeatherIdBean weather_id) {
                    this.weather_id = weather_id;
                }

                public String getWeek() {
                    return week;
                }

                public void setWeek(String week) {
                    this.week = week;
                }

                public String getWind() {
                    return wind;
                }

                public void setWind(String wind) {
                    this.wind = wind;
                }

                public static class WeatherIdBean {
                    private String fa;
                    private String fb;

                    public String getFa() {
                        return fa;
                    }

                    public void setFa(String fa) {
                        this.fa = fa;
                    }

                    public String getFb() {
                        return fb;
                    }

                    public void setFb(String fb) {
                        this.fb = fb;
                    }
                }
            }

            public static class Day20170325Bean {
                private String date;
                private String temperature;
                private String weather;
                /**
                 * fa : 01
                 * fb : 02
                 */

                private WeatherIdBean weather_id;
                private String week;
                private String wind;

                public String getDate() {
                    return date;
                }

                public void setDate(String date) {
                    this.date = date;
                }

                public String getTemperature() {
                    return temperature;
                }

                public void setTemperature(String temperature) {
                    this.temperature = temperature;
                }

                public String getWeather() {
                    return weather;
                }

                public void setWeather(String weather) {
                    this.weather = weather;
                }

                public WeatherIdBean getWeather_id() {
                    return weather_id;
                }

                public void setWeather_id(WeatherIdBean weather_id) {
                    this.weather_id = weather_id;
                }

                public String getWeek() {
                    return week;
                }

                public void setWeek(String week) {
                    this.week = week;
                }

                public String getWind() {
                    return wind;
                }

                public void setWind(String wind) {
                    this.wind = wind;
                }

                public static class WeatherIdBean {
                    private String fa;
                    private String fb;

                    public String getFa() {
                        return fa;
                    }

                    public void setFa(String fa) {
                        this.fa = fa;
                    }

                    public String getFb() {
                        return fb;
                    }

                    public void setFb(String fb) {
                        this.fb = fb;
                    }
                }
            }

            public static class Day20170326Bean {
                private String date;
                private String temperature;
                private String weather;
                /**
                 * fa : 01
                 * fb : 02
                 */

                private WeatherIdBean weather_id;
                private String week;
                private String wind;

                public String getDate() {
                    return date;
                }

                public void setDate(String date) {
                    this.date = date;
                }

                public String getTemperature() {
                    return temperature;
                }

                public void setTemperature(String temperature) {
                    this.temperature = temperature;
                }

                public String getWeather() {
                    return weather;
                }

                public void setWeather(String weather) {
                    this.weather = weather;
                }

                public WeatherIdBean getWeather_id() {
                    return weather_id;
                }

                public void setWeather_id(WeatherIdBean weather_id) {
                    this.weather_id = weather_id;
                }

                public String getWeek() {
                    return week;
                }

                public void setWeek(String week) {
                    this.week = week;
                }

                public String getWind() {
                    return wind;
                }

                public void setWind(String wind) {
                    this.wind = wind;
                }

                public static class WeatherIdBean {
                    private String fa;
                    private String fb;

                    public String getFa() {
                        return fa;
                    }

                    public void setFa(String fa) {
                        this.fa = fa;
                    }

                    public String getFb() {
                        return fb;
                    }

                    public void setFb(String fb) {
                        this.fb = fb;
                    }
                }
            }
        }

        public static class SkBean {
            private String humidity;
            private String temp;
            private String time;
            private String wind_direction;
            private String wind_strength;

            public String getHumidity() {
                return humidity;
            }

            public void setHumidity(String humidity) {
                this.humidity = humidity;
            }

            public String getTemp() {
                return temp;
            }

            public void setTemp(String temp) {
                this.temp = temp;
            }

            public String getTime() {
                return time;
            }

            public void setTime(String time) {
                this.time = time;
            }

            public String getWind_direction() {
                return wind_direction;
            }

            public void setWind_direction(String wind_direction) {
                this.wind_direction = wind_direction;
            }

            public String getWind_strength() {
                return wind_strength;
            }

            public void setWind_strength(String wind_strength) {
                this.wind_strength = wind_strength;
            }
        }

        public static class TodayBean {
            private String city;
            private String comfort_index;
            private String date_y;
            private String dressing_advice;
            private String dressing_index;
            private String drying_index;
            private String exercise_index;
            private String temperature;
            private String travel_index;
            private String uv_index;
            private String wash_index;
            private String weather;
            /**
             * fa : 02
             * fb : 01
             */

            private WeatherIdBean weather_id;
            private String week;
            private String wind;

            public String getCity() {
                return city;
            }

            public void setCity(String city) {
                this.city = city;
            }

            public String getComfort_index() {
                return comfort_index;
            }

            public void setComfort_index(String comfort_index) {
                this.comfort_index = comfort_index;
            }

            public String getDate_y() {
                return date_y;
            }

            public void setDate_y(String date_y) {
                this.date_y = date_y;
            }

            public String getDressing_advice() {
                return dressing_advice;
            }

            public void setDressing_advice(String dressing_advice) {
                this.dressing_advice = dressing_advice;
            }

            public String getDressing_index() {
                return dressing_index;
            }

            public void setDressing_index(String dressing_index) {
                this.dressing_index = dressing_index;
            }

            public String getDrying_index() {
                return drying_index;
            }

            public void setDrying_index(String drying_index) {
                this.drying_index = drying_index;
            }

            public String getExercise_index() {
                return exercise_index;
            }

            public void setExercise_index(String exercise_index) {
                this.exercise_index = exercise_index;
            }

            public String getTemperature() {
                return temperature;
            }

            public void setTemperature(String temperature) {
                this.temperature = temperature;
            }

            public String getTravel_index() {
                return travel_index;
            }

            public void setTravel_index(String travel_index) {
                this.travel_index = travel_index;
            }

            public String getUv_index() {
                return uv_index;
            }

            public void setUv_index(String uv_index) {
                this.uv_index = uv_index;
            }

            public String getWash_index() {
                return wash_index;
            }

            public void setWash_index(String wash_index) {
                this.wash_index = wash_index;
            }

            public String getWeather() {
                return weather;
            }

            public void setWeather(String weather) {
                this.weather = weather;
            }

            public WeatherIdBean getWeather_id() {
                return weather_id;
            }

            public void setWeather_id(WeatherIdBean weather_id) {
                this.weather_id = weather_id;
            }

            public String getWeek() {
                return week;
            }

            public void setWeek(String week) {
                this.week = week;
            }

            public String getWind() {
                return wind;
            }

            public void setWind(String wind) {
                this.wind = wind;
            }

            public static class WeatherIdBean {
                private String fa;
                private String fb;

                public String getFa() {
                    return fa;
                }

                public void setFa(String fa) {
                    this.fa = fa;
                }

                public String getFb() {
                    return fb;
                }

                public void setFb(String fb) {
                    this.fb = fb;
                }
            }
        }
    }
}

model请求网络接口:

public interface WeatherModel {
    
    public void load(String city,OnLoginListener loginListener);
    
}

model请求网络接口成功和失败等一些属性:

public interface OnLoginListener {

    void loginSuccess(Weather weather);

    void loginFailed(String s);
}

model请求数据:在子线程中请求,自己也可以封装请求框架(请求过来的数据直接封装到bean中)

public class WeatherImp implements WeatherModel {
    @Override
    public void load(final String city, final OnLoginListener loginListener) {
        //模拟子线程
        new Thread(){
            @Override
            public void run() {
                super.run();
                String url = "http://v.juhe.cn/weather/index?format=1&cityname="+city+"&key=d6257f63e0384eba477a218cc63c9a45";
                OkGo.get(url)
                        .tag(this)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String result, Call call, Response response) {
                               if (result!=null){
                                    Gson gson = new Gson();
                                    Weather weather = gson.fromJson(result, Weather.class);
                                    loginListener.loginSuccess(weather);
                                }else{
                                    loginListener.loginFailed("服务器里面的数据为空...");
                                }
                            }
                        });

            }
        }.start();
    }
}

view,实现抽象接口view(这个要根据业务逻辑来写),主要这个demo是天气的展示,所以我只要获取城市中的数据即可

public interface loadView {

    //获取城市
    String getCity();
    //获取的数据展示
    void toMainActivity(Weather weather);
    //弹出的Toast
    void ShowToast(String msg);
    //加载失败
    void showFailedError();

}

view中的activity的实现(其实MVP中的view即是activity)

public class HomeActivity extends BaseActivity implements loadView {

    @InjectView(R.id.city)
    TextView city;
    @InjectView(R.id.fragment)
    FrameLayout fragment;

    private WeatherPresener presener = new WeatherPresener(this);

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_home);
        ButterKnife.inject(this);
        presener.load();
    }

    @Override
    public void initViews() {

    }


    @Override
    public String getCity() {
        return "郑州";
    }

    //获取的数据更新UI
    @Override
    public void toMainActivity(Weather weather) {
        city.setText(weather.getResult().getToday().getCity());
    }

    @Override
    public void showFailedError() {
        ShowToast("加载失败...");
    }


    @OnClick(R.id.city)
    public void onClick() {
        Intent intent = new Intent(HomeActivity.this, ListViewActivity.class);
        startActivity(intent);
        finish();
    }

}

presenter:presenter就是model和view的交互桥梁(沟通两者肯定要在Presenter中new和创建来实现model和view)

public class WeatherPresener{

    private loadView loadView;
    private WeatherImp weatherImp;
    private Handler handler = new Handler();
    public WeatherPresener(loadView loadView){
        this.loadView = loadView;
        weatherImp = new WeatherImp();
    }

    public void load(){
        weatherImp.load(loadView.getCity(), new OnLoginListener() {
            @Override
            public void loginSuccess(final Weather weather) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        loadView.toMainActivity(weather);
                    }
                });
            }

            @Override
            public void loginFailed(String s) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            loadView.showFailedError();
                        }
                    });
            }
        });
    }


}

以上就实现了简单的mvp天气展示:demo下载地址:https://github.com/chenmiaohui/EveryDayWeather(里面还有写好的mvp的RecyclerView)

MVC和MVP区别:

通过上面例子可以看出mvp的view不能与model直接交互需要Presenter来控制,而mvc的view和model是相互的

两者的区别也很明显,项目中真实的用到mvp还是mvc,这个要根据项目要求和业务逻辑等一些方面来牵制,转载请署名出处!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MVCMVP 和 MVVM 是三种常见的软件架构模式,用于组织和管理应用程序的代码。它们有一些共同的概念,但也有一些区别。 MVC(Model-View-Controller)是最早提出的架构模式之一。在 MVC 中,应用程序被分为三个主要部分: 1. 模型(Model):负责处理数据和业务逻辑。 2. 视图(View):负责显示数据给用户。 3. 控制器(Controller):负责接收用户输入,并根据输入更新模型和视图。 MVP(Model-View-Presenter)是 MVC 模式的变体。MVP 将视图和控制器的职责分离,引入了一个新的组件 Presenter,它作为视图和模型之间的中介。MVP 的关键特点是: 1. 模型(Model):负责处理数据和业务逻辑。 2. 视图(View):负责显示数据给用户。 3. 主持人(Presenter):负责处理用户输入,并更新模型和视图。 MVVM(Model-View-ViewModel)是一种相对较新的架构模式,主要用于现代前端开发。MVVM 借鉴了 MVCMVP 的思想,并引入了数据绑定的概念。MVVM 的关键特点是: 1. 模型(Model):负责处理数据和业务逻辑。 2. 视图(View):负责显示数据给用户。 3. 视图模型(ViewModel):负责将模型数据转换为视图所需的格式,并处理用户输入。同时,它还通过数据绑定机制与视图保持同步。 总的来说,MVCMVP 和 MVVM 都是用于组织应用程序代码的架构模式,它们在组织代码和处理用户交互方面有一些区别。选择哪种架构模式取决于具体的应用场景和需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值