MVP设计模式Presenter

//BasePresenter

public abstract class BasePresenter {
    public ResponseInfoApi responseInfoApi;
    private HashMap<String,String> errorMap = new HashMap<>();
    public BasePresenter(){
        errorMap.put("1","错误类型一");
        errorMap.put("2","错误类型二");
        errorMap.put("3","错误类型三");
        errorMap.put("4","错误类型四");
        errorMap.put("5","错误类型五");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.BASEURL)//请求链接地址
                .addConverterFactory(GsonConverterFactory.create())//请求返回数据,以及需要用到解析的工具
                .build();//构建retrofit对象
        //如何发送请求方法
        //1.完整链接地址
        //2.请求方式
        //3.请求参数
        //4.请求结果数据用什么进行封装
        responseInfoApi = retrofit.create(ResponseInfoApi.class);
        //将请求发送出去
        /*Call<ResponseInfo> call = responseInfoApi.getHomeInfo("home");
        call.enqueue(callBack);*/
    }

    class CallBackAdapter implements Callback<ResponseInfo> {
        @Override
        public void onResponse(Call<ResponseInfo> call, Response<ResponseInfo> response) {
            //如果请求完成后服务器有响应
            ResponseInfo responseInfo = response.body();
            if (responseInfo.code.equals("0")){
                //data字段中的数据才是有意义的
                String json = responseInfo.data;
                //解析展示,无法在父类中具体实现,提供一个抽象方法,让子类实现
                parseJson(json);
            }else{
                //本次请求响应有异常
                String errorMessage = errorMap.get(responseInfo.code);
                onFailure(call,new RuntimeException(errorMessage));
            }
        }
        @Override
        public void onFailure(Call<ResponseInfo> call, Throwable t) {
            if (t instanceof RuntimeException){
                //t.getMessage()方法获取的就是RuntimeException中提供的参数
                sendErrorMessage(t.getMessage());
            }
            //请求失败方法,无服务没有给响应方法
            sendErrorMessage("请求异常");
        }
    }
    //留给子类处理错误方法
    protected abstract void sendErrorMessage(String errorMessage);
    //留给子类需要进行实现解析方法
    protected abstract void parseJson(String json) ;

    private void syncGetRequest() {
        new Thread(){
            @Override
            public void run() {
                try {
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(Constant.BASEURL)//请求链接地址
                            .addConverterFactory(GsonConverterFactory.create())//请求返回数据,以及需要用到解析的工具
                            .build();//构建retrofit对象
                    //如何发送请求方法
                    //1.完整链接地址
                    //2.请求方式
                    //3.请求参数
                    //4.请求结果数据用什么进行封装
                    ResponseInfoApi responseInfoApi = retrofit.create(ResponseInfoApi.class);
                    //将请求发送出去
                    Call<ResponseInfo> call = responseInfoApi.getHomeInfo("home");
                    //请求发出去后,得到一个响应体
                    Response<ResponseInfo> response = call.execute();//同步请求
                    //通过响应体获取数据
                    ResponseInfo responseInfo = response.body();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /*public interface GitHub {
        //@GET指定的是请求方式
        //@GET括号中()包含就是链接地址(方法传递参数形式指定)
        //@Path("owner") 指定的就是可变链接地址内容
        //请求返回值结果,由Call中的泛型指定
        @GET("/repos/{owner}/{repo}/contributors")
        Call<List<Contributor>> contributors(@Path("owner") String owner, @Path("repo") String repo);
    }*/
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值