Android MVC模式

       1、模型层(model):对数据库的操作、对网络等的操作都应该在model里面处理,对业务计算等操作也是必须放在的该层的。
  2、视图层(view):一般采用xml文件进行界面的描述,使用的时候可以非常方便的引入,在android中也可以使用javascript+html等的方式作为view层,这里需要进行java和javascript之间的通信,android提供了它们之间非常方便的通信实现。
  3、控制层(controller):android的控制层通常在acitvity,不要直接在acitivity中写代码,要通过activity交割model业务逻辑层处理, 这样做的另外一个原因是android中的acitivity的响应时间是5s,如果耗时的操作放在这里,程序就很容易被回收掉。

项目布局

1.布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RadioGroup
        android:id="@+id/rg_57"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="五言诗" />

        <RadioButton
            android:id="@+id/rb_7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="七言诗" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/rg_ct"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_ct"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="藏头" />

        <RadioButton
            android:id="@+id/rb_cw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="藏尾" />

        <RadioButton
            android:id="@+id/rb_cz"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="藏中" />

        <RadioButton
            android:id="@+id/rb_dz"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="递增" />

        <RadioButton
            android:id="@+id/rb_dj"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="递减" />

    </RadioGroup>

    <RadioGroup
        android:id="@+id/rg_yy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_1y"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="双句一押" />

        <RadioButton
            android:id="@+id/rb_2y"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="双句押韵" />

        <RadioButton
            android:id="@+id/rb_3y"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="一三四押" />
    </RadioGroup>

    <EditText
        android:id="@+id/et_key"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入藏头诗" />

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tv_show"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </ScrollView>


</LinearLayout>

2.model层

Bean对象

public class CangTouShiBean {


    /**
     * showapi_res_code : 0
     * showapi_res_error :
     * showapi_res_body : {"ret_code":0,"list":["北风勇士马,晚水独芙蓉。吾将宝非宝,英雄徒自强。","朝骑五花马,太华三芙蓉。吾将宝非宝,天子贵文强。","请歌牵白马,菡萏金芙蓉。大位天下宝,自从冒顿强。","青丝系五马,秀出九芙蓉。迈德惟家宝,日来知自强。","北买党项马,美女夸芙蓉。河宗来献宝,十年思自强。","青丝系五马,大嫂采芙蓉。药妙灵仙宝,不独有文强。"]}
     */

    private int showapi_res_code;
    private String showapi_res_error;
    private ShowapiResBodyBean showapi_res_body;


    @Override
    public String toString() {
        return "CangTouShiBean{" +
                "showapi_res_code=" + showapi_res_code +
                ", showapi_res_error='" + showapi_res_error + '\'' +
                ", showapi_res_body=" + showapi_res_body +
                '}';
    }

    public int getShowapi_res_code() {
        return showapi_res_code;
    }

    public void setShowapi_res_code(int showapi_res_code) {
        this.showapi_res_code = showapi_res_code;
    }

    public String getShowapi_res_error() {
        return showapi_res_error;
    }

    public void setShowapi_res_error(String showapi_res_error) {
        this.showapi_res_error = showapi_res_error;
    }

    public ShowapiResBodyBean getShowapi_res_body() {
        return showapi_res_body;
    }

    public void setShowapi_res_body(ShowapiResBodyBean showapi_res_body) {
        this.showapi_res_body = showapi_res_body;
    }

    public static class ShowapiResBodyBean {
        /**
         * ret_code : 0
         * list : ["北风勇士马,晚水独芙蓉。吾将宝非宝,英雄徒自强。","朝骑五花马,太华三芙蓉。吾将宝非宝,天子贵文强。","请歌牵白马,菡萏金芙蓉。大位天下宝,自从冒顿强。","青丝系五马,秀出九芙蓉。迈德惟家宝,日来知自强。","北买党项马,美女夸芙蓉。河宗来献宝,十年思自强。","青丝系五马,大嫂采芙蓉。药妙灵仙宝,不独有文强。"]
         */

        private int ret_code;
        private List<String> list;


        @Override
        public String toString() {
            return "ShowapiResBodyBean{" +
                    "ret_code=" + ret_code +
                    ", list=" + list +
                    '}';
        }

        public int getRet_code() {
            return ret_code;
        }

        public void setRet_code(int ret_code) {
            this.ret_code = ret_code;
        }

        public List<String> getList() {
            return list;
        }

        public void setList(List<String> list) {
            this.list = list;
        }
    }
}

请求回调接口

public interface BeanCallback<T> {

    void onError(String msg);

    void onSuccess(T t);
}

请求接口

public interface ICangTouShi {
    //请求数据,需要有变化的参数
    void doRequest(String num, String type, String yayuntype, String key, BeanCallback<CangTouShiBean> callback);
}

实现请求接口

public class CangTouShiModel implements ICangTouShi {
    @Override
    public void doRequest(String num, String type, String yayuntype, String key, final BeanCallback<CangTouShiBean> callback) {

        //请求数据
        //使用OkHttp

        OkHttpClient client = new OkHttpClient();

        RequestBody body = new FormBody.Builder()
                .add("showapi_appid","27306")
                .add("showapi_sign","150e9206e7f542bab4affe49d73cb920")
                .add("num",num)
                .add("type",type)
                .add("yayuntype",yayuntype)
                .add("key",key).build();

        Request request = new Request.Builder()
                .post(body)
                .url("http://route.showapi.com/950-1").build();
        Call call = client.newCall(request);
        //异步请求,子线程
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("TAG","-----------"+e.getMessage());
                callback.onError(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Gson gson = new Gson();
                CangTouShiBean bean = gson.fromJson(json, CangTouShiBean.class);
                callback.onSuccess(bean);
            }
        });

    }

}

3.controller层

public class MainActivity extends Activity {

    //逻辑判断,UI操作

    RadioGroup rg_57,rg_ct,rg_yy;
    EditText et_key;
    Button btn_submit;
    TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        registerListener();
    }

    private void registerListener() {
        //逻辑控制
        //实际上就只要监听提交按钮即可,因为其他的按钮只是获取数据,不需要按下后立即更改UI

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String key = et_key.getText().toString();
                if(TextUtils.isEmpty(key)){
                    Toast.makeText(MainActivity.this,"key不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                String num = rg_57.getCheckedRadioButtonId()==R.id.rb_5?"5":"7";
                String type = null;
                switch (rg_ct.getCheckedRadioButtonId()){
                    case R.id.rb_ct:
                        type = "1";
                        break;
                    case R.id.rb_cw:
                        type = "2";
                        break;
                    case R.id.rb_cz:
                        type = "3";
                        break;
                    case R.id.rb_dz:
                        type = "4";
                        break;
                    case R.id.rb_dj:
                        type = "5";
                        break;
                }
                String yy = null;
                switch (rg_yy.getCheckedRadioButtonId()){
                    case R.id.rb_1y:
                        yy="1";
                        break;
                    case R.id.rb_2y:
                        yy="2";
                        break;
                    case R.id.rb_3y:
                        yy="3";
                        break;
                }

                final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setTitle("提示");
                dialog.setMessage("开始请求");
                dialog.show();

                //请求数据
                CangTouShiModel model = new CangTouShiModel();
                //OkHttp的异步请求,在子线程中
                model.doRequest(num, type, yy, key, new BeanCallback<CangTouShiBean>() {
                    @Override
                    public void onError(String msg) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                                Toast.makeText(MainActivity.this,"msg",Toast.LENGTH_SHORT).show();

                            }
                        });
                    }

                    @Override
                    public void onSuccess(final CangTouShiBean bean) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                                List<String> list = bean.getShowapi_res_body().getList();
                                tv_show.setText("");
                                for (String s : list) {
                                    tv_show.append(s+"\n");
                                }

                            }
                        });

                    }
                });
            }
        });

    }

    private void initView() {
        rg_57 = (RadioGroup) findViewById(R.id.rg_57);
        rg_57.check(R.id.rb_5);
        rg_ct = (RadioGroup) findViewById(R.id.rg_ct);
        rg_ct.check(R.id.rb_ct);
        rg_yy = (RadioGroup) findViewById(R.id.rg_yy);
        rg_yy.check(R.id.rb_1y);
        et_key = (EditText) findViewById(R.id.et_key);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        tv_show = (TextView) findViewById(R.id.tv_show);


    }


}

写这个,只为自己了解

猜你喜欢

转载自blog.csdn.net/qq_30711091/article/details/83383176