博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
retrofit+MVP开发
阅读量:2145 次
发布时间:2019-04-30

本文共 3215 字,大约阅读时间需要 10 分钟。

项目结构:

视图层通过presenter调用模型来获取数据,模型调用retrofit获得数据后,再通过CallBack把数据返回给presenter,presenter通过Impl返回给view,view负责显示就行,逻辑层次明显。

首先要添加相关依赖:

compile 'com.squareup.retrofit2:retrofit:2.0.2'compile 'com.squareup.retrofit2:converter-gson:2.0.2'

model来实现业务操作:

public class PeotryModel {    //测试接口    private String POST_STR = "http://api.apiopen.top/";    //new 一个retrofit对象    Retrofit retrofit = new Retrofit.Builder().baseUrl(POST_STR)            .addConverterFactory(GsonConverterFactory.create())            .build();    //获取古诗信息    public void getPeotry(String name, final PeotryCallBack peotryCallBack) {        final PeotryApi peotryApi = retrofit.create(PeotryApi.class);        Call
peotryBeamCall = peotryApi.peotryBeamPost(name); peotryBeamCall.enqueue(new Callback
() { @Override public void onResponse(Call
call, Response
response) { PeotryBeam peotryBeam = response.body(); peotryCallBack.onSucceess(peotryBeam); } @Override public void onFailure(Call
call, Throwable t) { peotryCallBack.onFailed(); } }); }}

presenter进行传递:

public class PeotryPresenter {    private PeotryImpl peotryImpl;    private PeotryModel peotryModel;    public PeotryPresenter(PeotryImpl peotryImpl) {        this.peotryImpl = peotryImpl;        peotryModel = new PeotryModel();    }    public void getData(String name) {        peotryModel.getPeotry(name, new PeotryCallBack() {            @Override            public void onSucceess(PeotryBeam peotryBeam) {                peotryImpl.onSuccess(peotryBeam);            }            @Override            public void onFailed() {                peotryImpl.onFailed();            }        });    }}

view负责显示数据:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, PeotryImpl {    private EditText inputEt;    private Button getBtn;    private TextView resultTv;    private PeotryPresenter peotryPresenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        peotryPresenter = new PeotryPresenter(this);    }    private void initView() {        inputEt = findViewById(R.id.input_et);        getBtn = findViewById(R.id.get_btn);        resultTv = findViewById(R.id.result_tv);        getBtn.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.get_btn:                peotryPresenter.getData(getUserInput());                break;        }    }    @Override    public String getUserInput() {        return inputEt.getText().toString();    }    @Override    public void onSuccess(PeotryBeam peotryBeam) {        resultTv.setText(peotryBeam.getMessage() + "\n" + peotryBeam.getResult());    }    @Override    public void onFailed() {        resultTv.setText("获取失败");    }}

作者:Sam

资源地址:https://download.csdn.net/download/Jeyden_827/12264774

每日一笑:

情人节那天,老公深情的问老婆:宝贝儿,我送你什么好?老婆低着头笑着说:你送什么我都喜欢。老公含情脉脉的说:亲爱的,既然我送什么你都喜欢,那我送你回娘家吧!过几天我再接你回来。

欢迎关注公众号交流

转载地址:http://wwrgf.baihongyu.com/

你可能感兴趣的文章
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>
Intellij IDEA使用(六)—— 使用Intellij IDEA创建Java项目并配置jar包
查看>>
Eclipse使用(十)—— 使用Eclipse创建简单的Maven Java项目
查看>>
Eclipse使用(十一)—— 使用Eclipse创建简单的Maven JavaWeb项目
查看>>
Intellij IDEA使用(十三)—— 在Intellij IDEA中配置Maven
查看>>
面试题 —— 关于main方法的十个面试题
查看>>
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
Redis学习笔记(四)—— redis的常用命令和五大数据类型的简单使用
查看>>
Win10+VS2015编译libcurl
查看>>
Windows下使用jsoncpp
查看>>
Ubuntu下测试使用Nginx+uWsgi+Django
查看>>
Windows下编译x264
查看>>
visual studio调试内存泄漏工具
查看>>