MVC

一、MVC:Model-View-Controller(模型-视图-控制)

Model:模型层用来处理数据
View:用来显示
Controller:用来控制M和V

代码部分

M:

1、下载接口 intereface getJson(String url)
2、结果接口 intereface success(String json);fail(int errorCode);
3、下载接口的实现类:
(1)写具体的下载逻辑,重写getJson(){下载的具体代码}
(2)下载的结果传给结果接口,调用结果接口Rinterface.success()

V、C(Activity)

1、调用下载的实现类,传递url和结果接口
2、重写success方法取结果

二、postMan:用来测试url,装在谷歌浏览器的插件

三、RestfulAPI:模拟化url,把url分成若干份,复用方便管理

MVC请求Json字符串案例

(两个接口,一个类,一个activity)

第一个接口,得到url

public interface GetJsonInterface {
    void getJson(String url);
}

第二个接口,成功和失败方法

public interface ResultInterface {
    void success();
    void file();
}

一个类,实现第一个接口,重写方法,并返回请求结果

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();
        final Request request = new Request.Builder().get().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                resultInterface.file();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                resultInterface.success();
            }
        });
    }
}

Activity

public class MainActivity extends AppCompatActivity implements ResultInterface{

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        GetJsonImpl getJson = new GetJsonImpl(this);
        getJson.getJson("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1");
    }

    @Override
    public void success() {
        Log.i(TAG, "success: ");
    }

    @Override
    public void file() {
        Log.i(TAG, "file: ");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值