MVC架构

一:MVC的介绍

  MVC是Model-View-Controller的简称
  
  Model:模型层,负责处理数据的加载或者存储 

  View:视图层,负责界面数据的展示,与用户进行交互 

  Controller:控制器层,负责逻辑业务的处理

二:MVC的特点

  1、耦合性低。降低了代码的耦合性,利用MVC框架使得View(视图)层和Model(模型)层可以很好的分离,这样就达到了解耦的目的,所以耦合性低,减少模块代码之间的相互影响。 
  2、模块区域分明,方便开发人员的维护。

三:使用MVC架构获取json字符串

在这里插入图片描述

Model代码

规定如何处理数据 M层第一步接口


public interface GetJsonInterface {

    public void getJson(String url);

}

Medol层的第二步,接口的实现 类
给接口传值,调用接口中的方法

public class GetJsonImpl implements GetJsonInterface {

    ResultInterface resultInterface;

    //传值
    public GetJsonImpl(ResultInterface resultInterface) {
        this.resultInterface = resultInterface;
    }

    @Override
    public void getJson(String url) {
        OkHttpClient client = new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(url).get().build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                resultInterface.fail();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //成功
                String string = response.body().string();
                Log.d("jsonImpl",string);
                resultInterface.success(string);
            }
        });
    }
}

返回结果接口

public interface ResultInterface {

    public void success(String json);

    public void fail();

}

Acitivity代码(VC)

public class MainActivity extends AppCompatActivity implements ResultInterface{

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

    public void download(View view) {
        //下载的逻辑写了,在Modle
        //显示 还得和Medol

        GetJsonImpl im = new GetJsonImpl(this);
        //执行下载的逻辑
        im.getJson("https://news-at.zhihu.com/api/4/news/latest");
    }

    //取值
    @Override
    public void success(String json) {//去结果,从M层的取的
        Log.d("json",json);
    }

    @Override
    public void fail() {

    }
}

四:postMan

用来测试url,装在谷歌流浪器的插件

五:RestfulAPI作用

模块化URL,把URL 分成若干份,复用方便管理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值