retrofit+MVP开发

项目结构:

视图层通过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<PeotryBeam> peotryBeamCall = peotryApi.peotryBeamPost(name);
        peotryBeamCall.enqueue(new Callback<PeotryBeam>() {
            @Override
            public void onResponse(Call<PeotryBeam> call, Response<PeotryBeam> response) {
                PeotryBeam peotryBeam = response.body();
                peotryCallBack.onSucceess(peotryBeam);
            }

            @Override
            public void onFailure(Call<PeotryBeam> 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

每日一笑:

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

欢迎关注公众号交流
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值