MVP实现OkHttp+RecyclerView

所需依赖

  /*glide图片工具*/
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  /*gson解析*/
  implementation 'com.google.code.gson:gson:2.2.4'
  /*okhttp网络请求*/
  implementation 'com.squareup.okhttp3:okhttp:3.12.0'
  /*recyclerview展示*/
  implementation 'com.android.support:recyclerview-v7:28.0.0'

HttpUtils

  //定义接口
    public interface HttpCallBack {
        //获取数据
        void getData(String s);

        //返回数据
        void setData(List<HttpUser.DataBean> data);
    }

    public HttpUtils jie(String path, final HttpCallBack callBack) {
        //new OkHttpClient;
        OkHttpClient client = new OkHttpClient();
        //构造Request对象
        Request request = new Request.Builder().get().url(path).build();
        //通过前两步构造Call对象
        Call call = client.newCall(request);
        //通过Call-enqueue(CallBack)方法异步提交请求
        call.enqueue(new Callback() {
            //失败
            @Override
            public void onFailure(Call call, IOException e) {
            }

            //成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //获取请求码
                int code = response.code();
                //请求成功
                if (code == 200) {
                    //获取响应体-----将java对象转化成json数据用
                    ResponseBody body = response.body();
                    //转化String类型
                    String string = body.string();
                    //获取数据
                    callBack.getData(string);
                }
            }
        });
        return this;
    }

DataCall

 //正确返回
 void onSuccess(String result);
 //错误返回
void onFailer(String msg);
 //获取数据
 void onGetData(List<HttpUser.DataBean> data);

Model

//定义方法--传入网址 工具类接口
   public void jie(String path, final HttpUtils.HttpCallBack callBack) {
       //获取工具类对象
       HttpUtils httpUtils = new HttpUtils();
       //工具类接口调用
       httpUtils.jie(path, new HttpUtils.HttpCallBack() {
           @Override
           public void getData(String s) {
               //Gson解析
               Gson gson = new Gson();
               HttpUser user = gson.fromJson(s, HttpUser.class);
               //存入集合
               List<HttpUser.DataBean> data = user.getData();
               //返回数据
               callBack.setData(data);
           }
           @Override
           public void setData(List<HttpUser.DataBean> data) {
           }
       });
   }

Presenter

 private DataCall call;
 private HttpModel mHttpModel;
 private List<HttpUser.DataBean> mdata;
  //重写P-传入core接口
   public HttpPresenter(DataCall dataCall) {
       call = dataCall;
       //实例化M层对象
       mHttpModel = new HttpModel();
   }
   //定义方法 调M层方法-----传接口
   public void jie(String path) {
       mHttpModel.jie(path, new HttpUtils.HttpCallBack() {
           @Override
           public void getData(String s) {
               //判断数据
               if (s.equals("成功的返回")) {
                   call.onSuccess("成功的返回");
               } else {
                   call.onFailer("返回失败");
               }
           }
           @Override
           public void setData(List<HttpUser.DataBean> data) {
               //传入数据
               mdata = data;
               call.onGetData(mdata);
           }
       });
   }

Activity

private RecyclerView mReyc;
private HttpPresenter presenter;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       //初始化控件
       initView();
       //P层对象
       presenter = new HttpPresenter(this);
       //传入接口
       presenter.jie("http://www.xieast.com/api/news/news.php?page=1");
       //线性布局
//      LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//      mReyc.setLayoutManager(linearLayoutManager);
       //Resc--瀑布流
      initLayout(false, true);
   }
   private void initLayout(boolean a, boolean b) {
       //瀑布流布局
       StaggeredGridLayoutManager staggeredGridLayoutManager = new
       StaggeredGridLayoutManager(3, b ? StaggeredGridLayoutManager.VERTICAL : StaggeredGridLayoutManager.HORIZONTAL);
       //设置布局
       mReyc.setLayoutManager(staggeredGridLayoutManager);
          }
          
   //初始化控件
   private void initView() {
       mReyc = findViewById(R.id.reyc);
    }
    
   @Override
   public void onSuccess(String result) {
       Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
   }
   
   @Override
   public void onFailer(String msg) {
       Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
   }

    @Override
   public void onGetData(final List<HttpUser.DataBean> data) {
       //更新UI主线程
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               //设置适配器
               HttpAdapter adapter = new HttpAdapter(getApplicationContext(), data);
               //数据点击
               adapter.setClick(new HttpAdapter.RecycleitemClick() {
                   @Override
                   public void onItemClick(View view, int position) {
                       Toast.makeText(MainActivity.this, "第" + position + "条数据", Toast.LENGTH_SHORT).show();
                   }
               });
               mReyc.setAdapter(adapter);
           }
       });
   }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值